1
votes

Les méthodes de UITabBar Lifecycle ne sont pas déclenchées à partir du démarrage en arrière-plan

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)


0 commentaires

3 Réponses :


0
votes

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)


0 commentaires

1
votes

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


1 commentaires

N'oubliez pas de supprimer l'observateur chaque fois que vous naviguez vers d'autres écrans.



0
votes

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


0 commentaires