【问题标题】:How to stop updating location when user presses logout button in swift当用户在swift中按下注销按钮时如何停止更新位置
【发布时间】: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

标签: ios swift logout


【解决方案1】:

Logout_Button Clicked_Event 点击时间调用其他mapview控制器的NSTimer方法:

var MapVC:mapviewcontrolle = mapviewcontrolle() // diffrent view controller object
MapVC.timer.invalidate()  

【讨论】:

  • timer 显示错误....因为我使用不同的控制器我不能调用 timer...怎么办??
  • 创建控制器对象......并访问该计时器对象......调用该方法
  • 我正在尝试那个,但它显示我的错误,因为 mapviewcontroller 没有计时器....但是显示 func timetomoveon...我不知道为什么这会显示这个错误..我认为它没有采取,因为我在视图中声明了计时器确实加载了数学......是正确的吗??
猜你喜欢
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-23
  • 2015-07-20
  • 1970-01-01
  • 2016-08-17
相关资源
最近更新 更多