【发布时间】:2017-03-21 17:48:17
【问题描述】:
我正在使用 mapview 在 Swift 中开发一个应用程序,并使用 nsstimer 每 3 分钟获取一次当前位置。我可以获取位置并且一切正常。
我的 mapview 控制器中的示例代码:查看 didload 方法
let timer = NSTimer.scheduledTimerWithTimeInterval(180.0, target: self, selector: #selector(timeToMoveOn), userInfo: nil, repeats: true)
func timeToMoveOn()
{
print(latitude)
print(longitude)
}
每 3 分钟获取一次当前位置会有所帮助
在另一个视图控制器中编写的注销操作示例代码:
var viewcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
var appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as?
AppDelegate)!
appDelegate.window?.rootViewController = viewcontroller
appDelegate.window!.makeKeyAndVisible()
它也可以正常工作并且能够返回登录页面,但问题是当我点击注销操作时,func timeToMoveOn 会继续更新当前位置我不想在用户点击注销操作时更新位置.如何停止 NSTimer?
我搜索了其他 Stack Overflow 答案,但不支持我,因为我的 nstimer 和注销操作在不同的控制器中执行。有人可以帮我解决这个问题吗?
【问题讨论】:
-
timer?.invalidate() 调用注销时间
-
我必须在哪里包含此代码...以及如何调用注销时间您能简要解释一下...感谢您的快速回复@jayesh miruliya