Методы UITabBar Lifecycle не запускаются с фонового запуска - PullRequest
1 голос
/ 27 мая 2019

В качестве основного контроллера у меня есть контроллер UITabBar с двумя вкладками. Каждая вкладка представляет собой NavigatorViewController со встроенным UIViewController.

Если я открою приложение из фона после предыдущего холодного запуска, ни один из ViewWillAppear (UITabBarController, UIViewController) не будет запущен.

Как я могу назвать жизненный цикл UITabBarChildren, когда пользователь пришел из backgroud? (IE: из уведомления)

Ответы [ 3 ]

0 голосов
/ 27 мая 2019

Это не в жизненном цикле, потому что состояние контроллеров не меняется во время фонового режима или других событий приложения.

Вы должны наблюдать за 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)
    }
}
0 голосов
/ 27 мая 2019

Вы можете добавить 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")
}
0 голосов
/ 27 мая 2019

Когда приложение происходит из фона, не вызывается viewWillAppear/viewDidAppear для любого активного виртуального канала, вам необходимо прослушать делегат приложения, например applicationWillEnterForegroundNotification

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

@objc func applicationWillEnterForegroundNotification(_ notification: NSNotification) {
  print("To-Do")
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...