【问题标题】:Adding additional UITabbarItem to UITabbarController将额外的 UITabbarItem 添加到 UITabbarController
【发布时间】:2011-01-29 10:04:50
【问题描述】:

我想知道是否有办法在我现有的UITabBarController 中添加一个额外的UITabBarItem。它不需要在运行时。

我想要做的就是点击这个按钮时,我想在我实际可见的 ViewController 上 presentModalViewController:,它应该是 TabBarController 或其控制器。

希望这足够清楚,如果没有,请随时询问。

【问题讨论】:

    标签: cocoa-touch uikit uitabbarcontroller uitabbaritem presentmodalviewcontroller


    【解决方案1】:

    根据我的研究,您无法将 UITabBarItem 添加到由 UITabBarController 管理的 UITabBar。

    由于您可能通过添加视图控制器列表来添加您的 UITabBarItem,这也是您选择添加更多自定义 UITabBarItems 的方式,我现在将展示:

    前置条件: 正如我之前提到的,您可能已经通过添加视图控制器列表来添加您的 UITabBarItems:

    tabbarController = [[UITabBarController alloc] init]; // tabbarController has to be defined in your header file
    
    FirstViewController *vc1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:[NSBundle mainBundle]];
    vc1.tabBarItem.title = @"First View Controller"; // Let the controller manage the UITabBarItem
    
    SecondViewController *vc2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
    vc2.tabBarItem.title = @"Second View Controller";
    
    [tabbarController setViewControllers:[NSArray arrayWithObjects: vc1, vc2, nil]];
    
    tabbarController.delegate = self; // do not forget to delegate events to our appdelegate
    

    添加自定义 UITabBarItems: 现在既然你知道如何通过添加视图控制器来添加 UITabBarItems,那么你也可以使用同样的方式来添加自定义的 UITabBarItems:

    UIViewController *tmpController = [[UIViewController alloc] init];
    tmpController.tabBarItem.title = @"Custom TabBar Item";
    // You could also add your custom image:
    // tmpController.tabBarItem.image = [UIImage alloc]; 
    // Define a custom tag (integers or enums only), so you can identify when it gets tapped:
    tmpController.tabBarItem.tag = 1;
    

    修改上一行:

    [tabbarController setViewControllers:[NSArray arrayWithObjects: vc1, vc2, tmpController, nil]];
    
    [tmpController release]; // do not forget to release the tmpController after adding to the list
    

    一切正常,现在您的 TabBar 中有自定义按钮。

    如何处理这个自定义 UITabBarItem 的事件?

    很简单,看:

    将 UITabBarControllerDelegate 添加到您的 AppDelegate 类(或持有 tabbarController 的类)。

    @interface YourAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> { }
    

    通过添加这个函数来适应协议定义:

    - (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
      NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
    
      UITabBarItem *item = [theTabBarController.tabBar.items objectAtIndex:indexOfTab];
      NSLog(@"Tab index = %u (%u), itemtag: %d", indexOfTab, item.tag);  
      switch (item.tag) {
        case 1:
          // Do your stuff
          break;
    
        default:
          break;
      }
    } 
    

    现在您拥有创建和处理自定义 UITabBarItems 所需的一切。 希望这可以帮助。 玩得开心....

    【讨论】:

      【解决方案2】:

      访问 UITabBarController (reference) 的 tabBar - 属性,使用 items - 属性 (reference) 获取元素数组,将新的 UITabBarItem 添加到此数组并使用 tabBar 的 setItems:animated: - 方法更新您的标签栏。向此选项卡栏添加一个操作以显示模态视图控制器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-26
        • 2021-01-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多