我想创建一个新的应用程序。在一定时间(即5秒)后,如何自动打开新的ViewController。
请解释答案,因为我是XCode的新手。
最佳答案
您可以为此使用NSTimer
类。
下面的代码行计划在特定时间(3秒)后调用特定方法:
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(myMethod) userInfo:nil repeats:NO];
在方法myMethod中,您可以简单地推送一个新的视图控制器:
- (void)myMethod
{
UIViewController *vc = [[UIViewController alloc] init];
[self pushViewController:vc animated:YES];
[vc release];
}
编辑:这是您可以使用Swift进行的操作:
var timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(MyClass.myMethod), userInfo: nil, repeats: false)
func myMethod()
{
let vc = UIViewController.init()
self.pushViewController(vc!, animated: true)
}