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

标签 iphone ios objective-c

我有三个选项卡栏应用程序,我的选项卡是 TAB1、TAB2、TAB3 我在 TAB1 View Controller 中编写了以下代码来检测用户按下了哪个选项卡

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

但是这个委托(delegate)永远不会被调用

我已经在 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];

}

最佳答案

您可以像这样检测选定的 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 
}

在.m 文件中

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


}

现在把这个 UITabbarController 的委托(delegate)放在

- (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;
    }
}

关于iphone - 切换标签栏时检测选定的标签栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14355367/

相关文章:

iphone - iOS UINavigationController

iphone - 优化快速重复调用的技巧(Objective-C,iPhone)?

ios - 无法将数据写入硬件外围设备

ios - ARC 的多线程自动释放问题?

objective-c - 等待 dispatch_semaphore 以等待许多异步任务完成的正确方法

ios - 计算依赖于异步加载的单元格宽度和图像的 UITableViewCell 的高度

javascript - 输入位置在 Iphone 上无法正确显示

ios - 如何按 2 列/键对对象的 NSArray 进行排序

objective-c - NSData 或来自 CMSampleBufferRef 的字节

ios - ShareKit 是否仍然有效 - 2015?如果可以,是否可以在 Swift 中实现?