在x秒之后恢复到之前的控制器

时间:2014-04-05 09:03:54

标签: ios objective-c uiviewcontroller timer

我有一个非常简单的应用程序,代码很少。在我的ViewController中,我没有完成任何代码,我只添加了一个导航栏,其中包含一个带有VideoController模态的下一个按钮。我想要实现的是在ViewController中按下下一个按钮后,允许用户查看VideoController 5秒钟,然后自动返回到上一个ViewController。这是我在VideoController.m中添加的代码

#import "VideoController.h"

@interface VideoController ()

@end

@implementation VideoController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
 //code added
[self performSelector:@selector(goBack) withObject:nil afterDelay:5.0];


 }


 //code added
-(void)goBack{
 [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
\
@end

我没有收到任何错误,但是这并没有按照需要将我恢复到上一页。我错过了什么?

1 个答案:

答案 0 :(得分:0)

试试这段代码

- (void)viewDidLoad
  {
    [super viewDidLoad];
    //code added
    //[self performSelector:@selector(goBack) withObject:nil afterDelay:5.0];
    [NSTimer scheduledTimerWithTimeInterval:3.0
                           target:self
                           selector:@selector(goBack)
                           userInfo:nil repeats:NO];

}

 //code added
 -(void)goBack{
     [self.navigationController popViewControllerAnimated:YES];
 }