gpt4 book ai didi

ios - 使用 NSTimer 注销应用程序

转载 作者:行者123 更新时间:2023-11-29 01:04:08 26 4
gpt4 key购买 nike

我有一个登录页面,该页面会转到应用程序的其余部分。在我的应用程序的其余部分,其余的 View Controller 从我自己定义的“BaseViewController”扩展而来。

我有以下问题?

 import UIKit

class BsgBaseViewController: UIViewController {
var nsTimerObj: NSTimer!
func resetTimer(addTimeToTimeOutThread: NSTimeInterval){
nsTimerObj?.invalidate()

let nsTimerObjTemp = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: "handleIdleEvent:", userInfo: nil, repeats: false)
nsTimerObj = nsTimerObjTemp

}


func handleIdleEvent(timer: NSTimer) {

let createAccountErrorAlert: UIAlertView = UIAlertView()
createAccountErrorAlert.delegate = self
createAccountErrorAlert.title = ("Title")
createAccountErrorAlert.message = "Due to the security policies your session has been timed out. We have to log you out."
createAccountErrorAlert.addButtonWithTitle("OK")
createAccountErrorAlert.show()

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("user touched")
self.resetTimer(HardCodedValues().hardCodedTimeOutApplication)
}

override func viewDidAppear(animated: Bool) {
self.resetTimer(HardCodedValues().hardCodedTimeOutApplication)
print("appear")
}

override func viewWillDisappear(animated: Bool) {
self.resetTimer(HardCodedValues().hardCodedTimeOutApplication)
print("disappear")
}

}
  1. 当单击 UITextFieldUITextView 时,方法 touchesBegan() 不会被触发。

  2. 我有一个设置 UIViewController 和一个带有两个容器和两个 UIViewControllerUISegmentedControl。所有这三个 UIViewController 都派生 self 的 BaseViewController。当我点击另外两个 UIViewController 时,会创建三个 BaseViewController 实例,我想避免这样做。

最佳答案

想到的第一个想法是发明一个 InactivityMonitor 类并让应用程序委托(delegate)创建它的一个实例。让它拥有计时器并监听某种“刷新”通知。

View Controller 可以在触摸时发布这些通知,以便计时器可以重置。

您可能还需要将 Controller 作为文本字段和 View 的代理,以便它们也可以发布有关此类事件的通知。事实上,最困难的部分可能是识别需要注意的所有用户事件,具体取决于您拥有的 UI 元素类型。

我想考虑的另一件事是您的规则应如何应用于后台/前台应用程序事件。

关于ios - 使用 NSTimer 注销应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36623033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com