我想知道是否有一种方法可以添加额外的UITabBarItem
到我现有的UITabBarController
。它不需要在运行时。添加额外的UITabbarItem到UITabbarController
我想要做的就是按下这个按钮时,我想要presentModalViewController:
超过我实际上可见的ViewController,它应该是TabBarController或它的控制器。
希望这是足够清楚的,如果不是,请随时问。
我想知道是否有一种方法可以添加额外的UITabBarItem
到我现有的UITabBarController
。它不需要在运行时。添加额外的UITabbarItem到UITabbarController
我想要做的就是按下这个按钮时,我想要presentModalViewController:
超过我实际上可见的ViewController,它应该是TabBarController或它的控制器。
希望这是足够清楚的,如果不是,请随时问。
作为我的研究结果,您不能将UITabBarItem添加到由UITabBarController管理的UITabBar。
既然你也许已经通过添加视图控制器列表中所增加的UITabBarItem,这也是你的选择,以进一步增加自定义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 现在既然你知道如何添加UITabB通过添加视图控制器arItems,你也可以用同样的方式来添加自定义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。 希望这有助于。 玩得开心....