简体   繁体   English

用户快速按下注销按钮时如何停止更新位置

[英]How to stop updating location when user presses logout button in swift

I am developing a application in Swift using mapview and am fetching current location for every 3 minutes using nsstimer. 我正在使用mapview在Swift中开发一个应用程序,并使用nsstimer每3分钟获取一次当前位置。 I can able to fetch location and everything works fine. 我可以获取位置,并且一切正常。

Sample code in my mapview controller:In view didload method 我的mapview控制器中的示例代码:在视图didload方法中

let timer = NSTimer.scheduledTimerWithTimeInterval(180.0, target: self, selector: #selector(timeToMoveOn), userInfo: nil, repeats: true)

func timeToMoveOn()
{
 print(latitude)

 print(longitude)

}

It helps to fetch current location for every 3 minutes 每3分钟可获取一次当前位置

Sample code for logout action which written in another view controller: 在另一个视图控制器中编写的注销操作的示例代码:

  var viewcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
  var appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as?
        AppDelegate)!
    appDelegate.window?.rootViewController = viewcontroller
    appDelegate.window!.makeKeyAndVisible()

It also works fine and am able to return to login page, but the PROBLEM is when I tapped logout action that func timeToMoveOn is keep on updating current location I don't want to update location when the user tapped logout action. 它也可以正常工作,并且能够返回登录页面,但是问题是当我点击注销操作时func timeToMoveOn一直在更新当前位置时,我不想在用户点击注销操作时更新位置。 How to stop NSTimer? 如何停止NSTimer?

I have searched other Stack Overflow answers and that does not support me because my nstimer and logout action are performing in different controllers. 我搜索了其他Stack Overflow答案,但不支持我,因为我的nstimer和注销操作在不同的控制器中执行。 Can someone help me out to solve this problem? 有人可以帮我解决这个问题吗?

Logout_Button Clicked_Event click time call that NSTimer method of other mapview controller : 其他mapview控制器的NSTimer方法的Logout_Button Clicked_Event点击时间调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保存用户快速按下按钮的日期? - How to save the date the user presses a button in swift? Swift tvOS-用户按下MENU按钮时暂停UIImage动画 - Swift tvOS - pause UIImage animations when user presses MENU button 当用户按下按钮时,如何在我的Swift应用程序中切换到另一个viewController? - How can I switch to another viewController in my swift app when user presses the button? 一个额外的popViewControllerAnimated:当用户按下“后退”按钮时? - An extra popViewControllerAnimated: when the user presses the Back button? 检测用户何时按下空格键 - Detect when a user presses the space button 在后台快速更新用户位置 - updating the user location in the background swift 用户按下后以编程方式更改栏按钮项目 Swift - Change bar button item programmatically after user presses it Swift 显示用户位置按钮 - Swift - Show user location button - Swift 如何检测用户何时按下幻灯片中的电源关闭按钮? - How can i detect when the user presses slide to power off button in iphone ? 当用户按下主页按钮并再次双击它以返回时,调用哪些方法 - Which methods are called when user presses home button and double presses it again to return
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM