NSNotification或Delegate在可见视图更改时注册

时间:2012-10-31 18:36:58

标签: objective-c ios uinavigationcontroller uitabbarcontroller

我正在使用Objective-c为ios开发一个项目,我有一个使用UITabBarController子类的多个选项卡的视图。每个选项卡都有自己的UINavigationController。当视图加载到选项卡上时,会触发相应的激活事件(viewWillAppear,viewDidLoad等)。但是,一旦点击其他选项卡,然后点按,则不会再次触发所有这些事件,因为该视图已经是该特定选项卡的可见视图(例如viewDidLoad)。

我的问题是:是否有通知或委托我只需注册并在窗口中的可见视图发生变化时收到通知?我做了一些研究,但没有找到任何具体的研究。我打算做的是:

  1. 选项卡栏索引更改时检查可见视图:tabBarController:didSelectViewController
  2. 在每个导航控制器上注册此事件:navigationController:didShowViewController:animated:
  3. 通过这样做,每当visibleViewController通过更改选项卡或在选项卡的导航流程中导航(模态除外,在这种情况下,我不关心它们。)它应该会得到通知。它们已被处理

    这是正确的方法吗?

2 个答案:

答案 0 :(得分: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

希望有所帮助!

答案 1 :(得分:0)

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

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