J'ai un contrôleur UITabBar
comme contrôleur principal, avec 2 onglets. Chaque onglet est un NavigatorViewController
avec un UIViewController
intégré.
Si j'ouvre l'application en arrière-plan après un précédent lancement à froid, aucun des ViewWillAppear
( UITabBarController
, UIViewController
) n'est déclenché. < / p>
Comment puis-je appeler le cycle de vie de UITabBarChildren lorsque l'utilisateur vient de backgroud? (IE: à partir d'une notification)
3 Réponses :
Lorsque l'application vient de l'arrière-plan non viewWillAppear / viewDidAppear
est appelée pour n'importe quel vc actif, vous devez écouter le délégué d'application comme applicationWillEnterForegroundNotification
@objc func applicationWillEnterForegroundNotification(_ notification: NSNotification) { print("To-Do") }
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForegroundNotification), name: UIApplication.willEnterForegroundNotification, object: nil)
Cela ne fait pas partie du cycle de vie car l'état des contrôleurs ne change pas pendant le mode d'arrière-plan ou d'autres événements d'application.
Vous devriez observer pour 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) } }
N'oubliez pas de supprimer l'observateur chaque fois que vous naviguez vers d'autres écrans.
Vous pouvez ajouter un observateur
à UIApplicationWillEnterForeground
dans vos contrôleurs
.
Publié peu de temps avant qu'une application ne quitte l'état d'arrière-plan pour devenir l'application active.
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") }