【发布时间】:2016-04-14 12:24:26
【问题描述】:
我有一个登录页面,它连接到应用程序的其余部分。 在我的应用程序的其余部分,其余视图控制器从我自己定义的“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")
}
}
单击
UITextField或UITextView时不会触发touchesBegan()方法。我有一个设置
UIViewController和一个UISegmentedControl,有两个容器和两个UIViewController。所有这三个UIViewController都来自我的BaseViewController。当我点击另外两个UIViewController时,三个BaseViewController实例被创建,这是我想避免的。
【问题讨论】:
-
第 2 点:您所看到的是派生对象的工作方式。子类是其父类的实现......加上一些东西。
-
@Phillip Mills 我想要实现的是在一段时间后注销我的应用程序。
-
没关系。我要说的是,如果您只希望应用程序有一个计时器,那么将计时器逻辑放在具有多个子类的基类中是没有意义的。在结构符合您的需要之前,它的作用以及如何重置的细节并不重要。
-
@Phillip Mills............我错了............我应该如何创建一个通用的 NSTimer整个应用程序的对象。
标签: ios swift uiviewcontroller nstimer uicontainerview