Ios UITabBar生命周期';的方法不会从后台启动
我有一个Ios UITabBar生命周期';的方法不会从后台启动,ios,swift,cocoa-touch,uitabbar,Ios,Swift,Cocoa Touch,Uitabbar,我有一个uitabar控制器作为主控制器,有两个选项卡。每个选项卡都是一个NavigatorViewController,其中嵌入了UIViewController 如果在上一次冷启动后从后台打开应用程序,则不会出现视图(UITabBarController,UIViewController) 当用户来自后台时,我如何调用uitabarchildren的生命周期?(即:来自通知)当应用程序来自后台时,对于任何活动的vc调用非viewWillDisplay/viewDidDisplay,您需要收听
uitabar
控制器作为主控制器,有两个选项卡。每个选项卡都是一个NavigatorViewController
,其中嵌入了UIViewController
如果在上一次冷启动后从后台打开应用程序,则不会出现视图
(UITabBarController
,UIViewController
)
当用户来自后台时,我如何调用uitabarchildren的生命周期?(即:来自通知)当应用程序来自后台时,对于任何活动的vc调用非
viewWillDisplay/viewDidDisplay
,您需要收听应用程序委托,如applicationWillEnterForegroundNotification
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForegroundNotification), name: UIApplication.willEnterForegroundNotification, object: nil)
class VC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Listen for application event somewhere early like `ViewDidLoad`
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForegroundNotification), name: UIApplication.willEnterForegroundNotification, object: nil)
}
// Implement a function that you want to execute when event happen
@objc func applicationWillEnterForegroundNotification() {
// Do anything before application Enter Foreground
}
// Remove observer when the controller is going to remove to prevent further issues
deinit {
NotificationCenter.default.removeObserver(self)
}
}
这不在生命周期中,因为在后台模式或其他应用程序事件期间,控制器的状态没有改变 您应该遵守
applicationWillEnterForegroundNotification
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForegroundNotification), name: UIApplication.willEnterForegroundNotification, object: nil)
class VC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Listen for application event somewhere early like `ViewDidLoad`
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForegroundNotification), name: UIApplication.willEnterForegroundNotification, object: nil)
}
// Implement a function that you want to execute when event happen
@objc func applicationWillEnterForegroundNotification() {
// Do anything before application Enter Foreground
}
// Remove observer when the controller is going to remove to prevent further issues
deinit {
NotificationCenter.default.removeObserver(self)
}
}
您可以在
控制器中向UIApplicationWillEnterForeground
添加一个观察者
在某个应用程序离开后台状态前往
成为活跃的应用程序
当您导航到其他屏幕时,不要忘记删除观察器。