向UITabbarController添加其他UITabbarItem [英] Adding additional UITabbarItem to UITabbarController
问题描述
我想知道是否有办法向现有的 UITabBarController
添加其他 UITabBarItem
。它不需要在运行时。
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.
我要做的就是单击此按钮时要 presentModalViewController:
在我实际可见的ViewController上,它应该是TabBarController或其控制器。
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.
推荐答案
根据我的研究,您无法将UITabBarItem添加到由UITabBarController管理的UITabBar。
As a result of my research you cannot add a UITabBarItem to a UITabBar that is managed by a UITabBarController.
由于您可能是通过添加视图控制器列表来添加UITabBarItem的,因此这也是您选择添加其他自定义UITabBarItems的方式,我现在将显示:
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:
前提条件:
如前所述,您可能已经通过添加视图控制器列表来添加了UITabBarItems:
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
添加自定义UITabBarItems:
现在,既然您知道如何通过添加视图控制器来添加UITabBarItems,则还可以使用相同的方法来添加自定义UITabBarItems:
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;
修改以上行:
[tabbarController setViewControllers:[NSArray arrayWithObjects: vc1, vc2, tmpController, nil]];
[tmpController release]; // do not forget to release the tmpController after adding to the list
之后释放tmpController很好,现在您有了
All fine, now you have your custom button in your TabBar.
如何处理此自定义UITabBarItem的事件?
很简单,看一下:
将UITabBarControllerDelegate添加到您的AppDelegate类(或持有tabbarController的类)中。
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;
}
}
现在,您拥有创建和处理自定义内容所需的一切UITabBarItems。
希望这会有所帮助。
玩得开心....
Now you have all you need to create and handle custom UITabBarItems. Hope this helps. Have fun....
这篇关于向UITabbarController添加其他UITabbarItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!