【发布时间】:2018-04-29 22:45:20
【问题描述】:
我的UIApplication 子类中运行了一个计时器,当它用完时应该将用户发送到某个ViewController。
我可以实例化我想去的ViewController...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "StartVC")
...但我不知道如何实际呈现它。在AppDelegate 内部我可以做window.rootViewController 等。但这在我的UIApplication 子类中不可用.
我也尝试过self.windows[0].rootViewController,但这始终只是第一个ViewController,它在应用程序启动时出现。与self.keyWindow.rootViewController 相同。老实说,我不知道这两个属性是什么。
上下文的完整代码:
import Foundation
import UIKit
class MyApplication: UIApplication {
var inactivityTimer: Timer!
override init() {
super.init()
restartInactivityTimer()
}
@objc func timerExceeded() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "StartVC")
//...here I would need to present "vc"
}
override func sendEvent(_ event: UIEvent) {
super.sendEvent(event)
restartInactivityTimer()
}
func restartInactivityTimer() {
if inactivityTimer != nil { inactivityTimer.invalidate() }
inactivityTimer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(timerExceeded), userInfo: nil, repeats: false)
}
}
【问题讨论】:
-
我能知道你为什么想要这样的东西吗?
-
@Sh_Khan 这是一个博物馆的应用程序。当被访问者放弃时需要自动返回开始屏幕。
标签: ios swift uiviewcontroller uiapplication