添加额外的 UITabbarItem 到 UITabbarController

发布于 2024-10-14 14:40:43 字数 233 浏览 3 评论 0原文

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

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

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

I am wondering if there is a way to add an additional UITabBarItem to my exisiting UITabBarController. It doesn't need to be in runtime.

All I want to do is when hitting this button I want to presentModalViewController: over my actually visible ViewController, which should either be the TabBarController or its controllers.

Hopefully this is clear enough, if not, feel free to ask.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月光色 2024-10-21 14:40:43

根据我的研究结果,您无法将 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 所需的一切。
希望这有帮助。
玩得开心....

As a result of my research you cannot add a UITabBarItem to a UITabBar that is managed by a UITabBarController.

Since you maybe have added your UITabBarItem by adding a list of view controller, this is also the way of your choice to add further custom UITabBarItems, as i will show now:

Pre-Conditions:
As i mentioned before, you maybe have added your UITabBarItems by adding a list of view controller:

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

Adding custom UITabBarItems:
Now since you know how to add UITabBarItems through adding view controller, you can also use the same way to add custom 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;

Modify the line above:

[tabbarController setViewControllers:[NSArray arrayWithObjects: vc1, vc2, tmpController, nil]];

[tmpController release]; // do not forget to release the tmpController after adding to the list

All fine, now you have your custom button in your TabBar.

What about handling events of this custom UITabBarItem?

Its easy, look:

Add the UITabBarControllerDelegate to your AppDelegate class (or the class which is holding the tabbarController).

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

Fit the protocol definition by adding this function:

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

Now you have all you need to create and handle custom UITabBarItems.
Hope this helps.
Have fun....

半葬歌 2024-10-21 14:40:43

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

Access the tabBar - property of your UITabBarController (reference), grab the elements array with the items - property (reference), add a new UITabBarItem to this array and use the tabBar's setItems:animated: - method to update your tab bar. Add an action to this tab bar to display the modal view controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文