我有这个问题。
如果您不使用故事板,请设置UITabBarController
代表在AppDelegate
是要走的路。然而,随着Storyboards
, the AppDelegate
不知道在哪里tabBarController
正在启动。你会想到subclassing
the tabBarController
并添加委托方法:
(void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController {
}
...就足够了。但是,令人恼火的是事实并非如此。
我需要知道用户何时按下选项卡按钮。我需要更多地了解这一点,因为我需要知道 viewController ”- (void)viewWillDisappear:(BOOL)animated {}
" 方法已运行。
我决定让我的UITabBarController
本身的代表。这对我来说似乎很愚蠢,但我做了以下事情......
#import <UIKit/UIKit.h>
@interface PlumbsTabBarController : UITabBarController <UITabBarControllerDelegate>
@end
然后,在我的viewDidLoad
方法,写了以下内容:
[self setDelegate:self];
这使得我的标签栏委托方法能够运行。
疯了还是什么?
好的 - 我现在正在编辑这个答案,尽管上面的内容都是正确的,但navigationController
正在使用,每个都选择tabBarButton
感动了,didSelectViewController
当您尝试时,委托方法将会NSLog(@"%@", viewController);
只显示您已选择UINavigationController
class?
因此,总的解决方案只是增加了更多的复杂性,就是子类化UINavigationController
对于每个viewController
你想要监控的,(做某事)当tabBarbutton
已被触动。
无论如何,它对我有用。而且,如果有人能对上述运球挑剔,他们可能会发现一个有用的方面 - 这对我来说就足够了 - 因为我发现这个网站也非常有用。