2

我有一个带有自定义 TabBar 控制器类的应用程序。

我试图实现标签栏控制器委托方法:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"%i",tabBarController.selectedIndex);
}

但它不起作用。为什么?

ViewDidLoad我写:

self.tabBarController.delegate = self;

在 .hi 中实现:

@interface BaseViewController : UITabBarController <UITabBarControllerDelegate>
4

2 回答 2

3

In your custom TabBarController, do not use

self.tabBarController.delegate = self;

But use

self.delegate = self;

.tabBarController returns the nearest ancestor in the view controller hierarchy that is a tab bar controller, but your custom TabBarController IS the controller you want to target, so no need to search in its hierarchy

于 2014-07-09T09:29:20.753 回答
1

您说过,这是您的自定义 TabBarController。你做了什么定制?如果您更改了 TabBar 面板并将其替换为您自己的以使用

setSelectedIndex:
setSelectedViewController:

手动方法,那么您也应该手动调用委托的方法。

根据Apple的文档

选项卡栏上可能发生两种类型的用户启动更改:

  • 用户可以选择选项卡。
  • 用户可以重新排列选项卡。

这两种类型的更改都会报告给标签栏控制器的委托,这是一个符合 UITabBarControllerDelegate 协议的对象。

还要检查UITabBarControllerDelegate 协议参考

在 iOS v3.0 及更高版本中,标签栏控制器会调用此方法,无论 > 选择的视图控制器是否发生变化。此外,它仅在用户点击 > 标签栏时调用,并且在您的代码以编程方式更改标签栏内容时不调用。

只有当用户与其 UITabBar 控件交互时,Delegate 才会响应。

于 2012-03-03T10:25:43.413 回答