我希望每次在应用程序中按下选项卡栏中的选项卡时都运行一个方法。我该怎么做呢?我在我的故事板中有一个标签栏控制器(当我创建项目时选择了标签栏应用程序时构建的)。
例如,我想在每次按下tab键时打印'tab键‘到控制台。
我试着在应用程序委托中添加这个,但没有成功:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSLog(@"Tab Button Pressed");
}这可能很简单,但我遗漏了一些东西。
发布于 2011-12-01 06:43:14
如果您使用的是Xcode4.2.x,那么模板将在appDelegate中的代码中设置UITabBarController
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.delegate = self; //<---- Add this line
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;如果您有一个旧样式的项目或使用接口构建器构建您的结构,那么:
对于
Tab bar controller,然后找到File's Owner的
delegate delegate插座


发布于 2011-12-01 03:40:42
您是否已将视图控制器设置为选项卡栏的代理?
https://stackoverflow.com/questions/8331879
复制相似问题