Microbob
Microbob

Reputation: 862

Detect when the View has been (re)shown

As the title suggests, how can I detect when a user sees a UIViewController. This is slightly different from viewDidLoad() and viewDidAppear(), both of which are run only when the app is first launched. What I am looking for is something like viewDidAppear(), but runs every time the view literally appears to the user (i.e. when the app is re-opened from the background or when the app is shown after the device is re-woken from sleep).

Thanks!

Upvotes: 0

Views: 908

Answers (1)

Tometoyou
Tometoyou

Reputation: 8386

viewDidAppear actually happens every time that view "appears", rather than just once on app load. For example, in an application that uses a UITabBarController, every time you press a tab, the view will switch to a specific view controller and the view controller's viewDidAppear method will get called.

To detect when the view appears from the background you need to register for notifications for applicationDidBecomeActive.

NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)

Upvotes: 1

Related Questions