我有一个登录页面,其中延伸到应用程序的其余部分。 在我的应用程序的其余部分中,剩余的视图控制器从我自己定义的“BaseViewController”中扩展。使用NSTimer注销应用程序
我有以下问题吗?
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")
}
}
touchesBegan()
上UITextField
或UITextView
点击时不会被解雇的方法。我有一个设置
UIViewController
与UISegmentedControl
与两个容器和两个UIViewController
。所有这三个UIViewController
都来自我的BaseViewController
。当我点击其他两个UIViewController
三个BaseViewController
的实例被创建,我想避免。
第2点:你看到的只是衍生物的工作方式。一个子类是它的父类的实现...加上一些东西。 –
@Phillip Mills我想要实现的是在一段时间后注销我的应用程序。 – Cloy
这很好。我说的是,如果你只需要一个定时器的应用程序,把计时器逻辑放在一个有多个孩子的基类中是没有意义的。它的功能和重置方式的细节在结构匹配你所需要的之前并不重要。 –