6

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?

4

2 回答 2

1

你看过 UITabBarControllerDelegate 吗?这种方法听起来你正在寻找什么:

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

从文档中:

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.

这是链接:http: //developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarControllerDelegate_Protocol/Reference/Reference.html

希望有帮助!

于 2012-10-31T19:08:40.500 回答
0

首先实现UITabBarController委托方法“tabBarController:didSelectViewController”并在app委托中注册。您不能在每个导航控制器中注册它。只有一个对象可以是委托。在该方法中,将其类型转换为 UINavigationController。

然后通过在 UINavigationController 上调用“topViewController”来获取 UIViewController。然后直接调用viewWillAppear:方法就可以了。

于 2012-10-31T21:33:24.640 回答