This question is an exact duplicate of:
Hello I am extremely new to xcode and programming in general. I am trying to create a simple app that has two view controllers. The first ViewController in the story board has no code only a navigation bar and a next button which has a modal connecting to VideoController. I have added this code to the VideoController.m file
- (void)viewDidLoad
{
[super viewDidLoad];
//code added
[NSTimer scheduledTimerWithTimeInterval:3.0
target:self
selector:@selector(goBack)
userInfo:nil repeats:NO];
}
//code added
-(void)goBack{
[self.navigationController popViewControllerAnimated:YES];
}
However this is not bringing me back to the previous page after the 3 second delay as i had hoped. Any suggestions? I dont know what I am missing
Edit - you don't really have to retain the timer, but retaining it will allow you to cancel it later by invalidating the timer.
First of all you have to retain your timer. Add it as a property to your class
in .h
in .m
Second you can use
dispatch_after
insteadAnd the last, do not do this. Whatever you are doing with your video controller, you don't want people to get thrown out of controllers for no reason. You probably should wait until the video has finished playing and dismiss your controller on the playback end notification.
You don't need to use a timer to fire an action once, GCD and
dispatch_after()
works juts as well without needing to use local variables to store the timer or to invalidate it.For example
If you use a segue to push the second view controller onto the stack you can dismiss it from the prepareForSegueMethod.
I've done it this way:
You can download a working example of this.