UITabBar Lifecycle的方法未从后台启动

时间:2019-05-27 11:10:51

标签: ios swift cocoa-touch uitabbar

我有一个UITabBar控制器作为主控制器,带有2个标签。每个标签都是嵌入了NavigatorViewController的{​​{1}}。

如果在上次冷启动后从后台打开应用程序,则不会触发UIViewControllerViewWillAppearUITabBarController)。

当用户来自backgroud时,如何调用UITabBarChildren的生命周期? (即:从通知中获取)

3 个答案:

答案 0 :(得分:1)

那不是生命周期,因为在后台模式或其他应用程序事件期间控制器的状态没有改变。

您应该注意applicationWillEnterForegroundNotification

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)
    }
}

答案 1 :(得分:0)

当应用程序来自非活动vc的非后台调用viewWillAppear/viewDidAppear时,您需要像applicationWillEnterForegroundNotification那样监听应用程序委托

NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForegroundNotification), name: UIApplication.willEnterForegroundNotification, object: nil)

@objc func applicationWillEnterForegroundNotification(_ notification: NSNotification) {
  print("To-Do")
}

答案 2 :(得分:0)

您可以在observer UIApplicationWillEnterForeground 中添加一个controllers

  

在应用离开背景状态之前不久发布   成为活动应用。

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self,selector: #selector(self.appEnteredFromBackground(_:)),name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
}

@objc func appEnteredFromBackground(_ notification: NSNotification) {
    print("From background")
}