开发者

Adding additional UITabbarItem to UITabbarController

开发者 https://www.devze.com 2023-02-07 04:07 出处:网络
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.

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 en开发者_Python百科ough, if not, feel free to ask.


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....


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消