Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSNotification or Delegate to register for when the visible view changes

I'm developing a project in objective-c for ios, and I have a view with multiple tabs using a subclass of UITabBarController. Each tab has it's own UINavigationController. When a view loads on a tab, the appropriate activation events fire (viewWillAppear, viewDidLoad, etc.). However, once you tap on a different tab, and tap back, not all these events will fire again since the view is already the visible view for that specific tab (viewDidLoad for example).

My question is this: is there a notification or delegate that I can simply register for and get notified when the visible view in the window changes? I've done some research and I didn't find anything specific for this. What I plan on doing is:

  1. Check the visible view when the tab bar index changes: tabBarController:didSelectViewController
  2. Register for this event on each navigation controller: navigationController:didShowViewController:animated:

By doing this, I should be notified whenever the visibleViewController changes by either changing the tab, or navigating within the tab's navigation flow (except for modals, in this case, I don't care about them. They are handled already).

Is this the right approach?

like image 952
Mark DiGiovanni Avatar asked Oct 31 '12 18:10

Mark DiGiovanni


1 Answers

Have you looked at UITabBarControllerDelegate? This method sounds what you are looking for:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

From the documentation:

In iOS v3.0 and later, the tab bar controller calls this method regardless 
of whether the selected view controller changed. In addition, it is called only
in response to user taps in the tab bar and is not called when your code 
changes the tab bar contents programmatically.

Here's the link: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarControllerDelegate_Protocol/Reference/Reference.html

Hope that helps!

like image 155
Brian Douglas Moakley Avatar answered Nov 15 '22 06:11

Brian Douglas Moakley