kub*_*lay 12 iphone objective-c ios5 uistoryboard uistoryboardsegue
在我的故事板中,我有一个视图作为启动画面.在这个视图中,我已经有一个像"打开应用程序"的按钮,它打开菜单视图,带有模态segue.但是我也希望屏幕自动执行segue,就像2秒后出现视图一样.
一些代码在这里:
- (void)viewDidAppear:(BOOL)animated
{
[self performSegueWithIdentifier:@"splashScreenSegue" sender:self];
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我已经使用performSegueWithIdentifier但它立即执行.有没有办法让它延迟?
提前致谢.
Mic*_*lum 25
您可以dispatch_after在视图出现后2秒使用GCD 执行您的segue代码,例如:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self performSegueWithIdentifier:@"splashScreenSegue" sender:self];
});
}
Run Code Online (Sandbox Code Playgroud)
另外,请确保在重写UIViewController的生命周期方法时记得调用super实现.
我认为有一种更好的方法可以让控制器本身处理调度问题.您可以像这样实现:
首先创建一个这样的方法,稍后您将使用它的选择器
- (void)showAnotherViewController{
[self performSegueWithIdentifier:@"yourSegueToAnotherViewController" sender:self];
}
Run Code Online (Sandbox Code Playgroud)
然后当你想在延迟后显示另一个视图控制器时,在当前视图控制器中使用这行代码:
[self performSelector:@selector(showAnotherViewController) withObject:nil afterDelay:yourDelayInSeconds];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9272 次 |
| 最近记录: |