设置N秒后执行一个函数,最常用的是设置一个定时器,今天刚看到有这样一个函数,感觉还是比较简单实用的,就先记下来,免得忘记了。
5秒后执行pushSecondController这个函数
[self performSelector:@selector(pushSecondController) withObject:nil afterDelay:5.0f];
比如:在第一个视图出现后5秒就跳转到第二个视图,代码如下:
- (void) pushSecondController{
SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:nil bundle:NULL];
[self.navigationController pushViewController:secondController animated:YES];
}
- (void) viewDidAppear:(BOOL)paramAnimated{
[super viewDidAppear:paramAnimated];
[self performSelector:@selector(pushSecondController) withObject:nil afterDelay:5.0f];
}
5秒钟后又跳回第一个页面
- (void) goBack{
[self.navigationController popViewControllerAnimated:YES];
}
- (void) viewDidAppear:(BOOL)paramAnimated{
[super viewDidAppear:paramAnimated];
[self performSelector:@selector(goBack) withObject:nil afterDelay:5.0f];
}