停止NSTimer并关闭视图控制器(swift) [英] Stop NSTimer and dismiss view controller (swift)

查看:82
本文介绍了停止NSTimer并关闭视图控制器(swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NSTimer 在我的 firebase 数据库中查找更新。我已将代码放在我的 viewDidLoad()中。

I am using NSTimer to look for updates in my firebase database. I have put the code inside my viewDidLoad().

NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: #selector(DriversInterfaceViewController.CheckFormularChild), userInfo: nil, repeats: true)

当用户收到数据库中的文件时,我希望用户转到另一个 ViewController 。问题是旧的 ViewController 在后台运行,或者在更改 View Controller 时计时器没有停止。

When the user has received a file in the database I want the user to goto another ViewController. The problem is that either the old ViewController is running in the background or the timer does not stop when changing View Controller.

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)            
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("recievedMission") as! RecievedMissionViewController
self.navigationController!.pushViewController(nextViewController, animated: true)

我如何关闭旧的 View Controller 或如何以编程方式停止 NSTimer ?谢谢!

How do I dismiss the old View Controller or how do I stop the NSTimer programmatically? Thanks!

推荐答案

只需存储计时器的实例,然后在移动到另一个控制器时使计时器失效,声明一个 NSTimer 的实例。

Just store the instance of your timer and then when you are moving to another Controller invalidate the timer, so that first declare one instance of NSTimer.

var timer:NSTimer?

现在使用此对象存储scheduledTimer的引用。

Now use this object to store the reference of your scheduledTimer.

self.timer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: #selector(DriversInterfaceViewController.CheckFormularChild), userInfo: nil, repeats: true)

现在简单地转移到另一个控制器。

Now on moving to another controller simply.

self.timer.invalidate()

这篇关于停止NSTimer并关闭视图控制器(swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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