计时器返回到 xcode 中的上一个视图 [英] Timer to return to previous view in xcode

查看:29
本文介绍了计时器返回到 xcode 中的上一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我对 xcode 和编程非常陌生.我正在尝试创建一个具有两个视图控制器的简单应用程序.故事板中的第一个 ViewController 没有代码,只有一个导航栏和一个具有连接到 VideoController 的模式的下一个按钮.我已将此代码添加到 VideoController.m 文件中

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];
}

然而,这并没有像我希望的那样在 3 秒延迟后让我回到上一页.有什么建议?我不知道我错过了什么

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

在.h

@property (strong, nonatomic) NSTimer *timer;

在.m

self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0
                       target:self
                       selector:@selector(goBack)
                       userInfo:nil repeats:NO];

第二,你可以使用 dispatch_after 代替

Second you can use dispatch_after instead

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    [self.navigationController popViewControllerAnimated:YES];
});

最后一点,不要这样做.无论您使用视频控制器做什么,您都不希望人们无缘无故地离开控制器.您可能应该等到视频播放完毕并在播放结束通知中关闭您的控制器.

And 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.

这篇关于计时器返回到 xcode 中的上一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆