切换标签栏时检测选定的标签栏

时间:2013-01-16 09:45:06

标签: iphone ios objective-c

我有三个标签栏应用程序,我的标签是TAB1,TAB2,TAB3我在TAB1视图控制器中编写了以下代码,以检测哪个标签用户已按下

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 
{ 
    NSLog(@"tab selected: %@", item.title); 
} 

但这个代表永远不会被称为

我在appdelegate.m中设置了我的标签

- (void)setupTabBar 
{
    self.myWorkListViewController = [[MyWorkListViewController alloc] initWithNibName:@"MyWorkListViewController"
                                                                               bundle:nil];
    self.askHRViewController = [[AskHRViewController alloc] initWithNibName:@"AskHRViewController"
                                                                     bundle:nil];

    self.moreViewController = [[MoreViewController alloc] initWithNibName:@"MoreViewController"
                                                                           bundle:nil];

    self.bookLeaveViewController = [[BookLeaveViewController alloc] initWithNibName:@"BookLeaveViewController"
                                                                             bundle:nil];
    self.helpViewController = [[HelpViewController alloc] initWithNibName:@"HelpViewController"
                                                                   bundle:nil];

    // Create navigation controllers
    workListNavigationController = [[UINavigationController alloc] initWithRootViewController:self.myWorkListViewController];

    askHRNavigationController = [[UINavigationController alloc] initWithRootViewController:self.askHRViewController];

    bookLeaveViewController = [[UINavigationController alloc] initWithRootViewController:self.bookLeaveViewController];

    moreNavigationController = [[UINavigationController alloc] initWithRootViewController:self.moreViewController];

    helpNavigationController = [[UINavigationController alloc] initWithRootViewController:self.helpViewController];

    [self setTabBarImagesAndText];

    // Setup tab bar controller
    self.tabBarController = [[UITabBarController alloc] init];
    [self.tabBarController setViewControllers:[NSArray arrayWithObjects:workListNavigationController, askHRNavigationController, bookLeaveViewController, helpNavigationController,moreNavigationController, nil]];
    //Set TabBar background
    [self.tabBarController.tabBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TabBar_iOS4_Background"]] atIndex:0];
    [self.tabBarController setSelectedIndex:0];

}

2 个答案:

答案 0 :(得分:5)

您可以像这样检测选定的Tabbar项目: - 与你的代码相同,你只需要添加这一行

// Setup tab bar controller
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate=self;
    [self.tabBarController setViewControllers:[NSArray arrayWithObjects:workListNavigationController, askHRNavigationController, bookLeaveViewController, helpNavigationController,moreNavigationController, nil]];
    //Set TabBar background
    [self.tabBarController.tabBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TabBar_iOS4_Background"]] atIndex:0];
    [self.tabBarController setSelectedIndex:0];
<。>在.h文件中定义如

@interface yourViewcontroller : UIViewController<UITabBarControllerDelegate>
{
   //declare your Tabbar controller with @proparty 
}
<。>文件中的

 //@synthesize here your Tabbar controller
- (void)viewDidLoad
{
  self.yourTabbarControler.delegate=self;
    [super viewDidLoad];


}

现在把这个UITabbarController委托给

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
    NSLog(@"controller title: %@", viewController.title);

    if (viewController == tabBarController.moreNavigationController)
    {
        tabBarController.moreNavigationController.delegate = self;
    }
}

答案 1 :(得分:0)

您必须使用代码(在ViewDidLoad等中)或在界面构建器中“连接”委托。
看看这个解释如何连接textView委托的答案(它几乎相同):https://stackoverflow.com/a/1785366/764575