从UINavigationController设置UITabBarItem标题? [英] Setting UITabBarItem title from UINavigationController?
问题描述
我设置了一个UITabBarController有两个选项卡,一个是一个简单的UIViewController和另一个是UINavigationController使用第二个UIViewController作为其rootController(这将稍后将用于设置UITableView)。我的问题是关于命名选项卡(即设置每个UITabBarItem的标题)
I have setup a UITabBarController with two tabs, one is a simple UIViewController and the other is a UINavigationController using a second UIViewController as its rootController (this will later be used to setup a UITableView). My question is with regard to naming the tabs (i.e. setting the title of each UITabBarItem)
对于第一个选项卡(简单的UIViewController)我添加了以下to the controllers -init method。
For the first tab (simple UIViewController) I have added the following (see below) to the controllers -init method.
// FROM -[MapController init]
- (id)init {
self = [super init];
if(self) {
UITabBarItem *tabBarItem = [self tabBarItem];
[tabBarItem setTitle:@"ONE"];
}
return self;
}
对于其他选项卡,我已经添加了(rootController)。
For the other tab I have added (see below) to the second controllers init (rootController).
// FROM -[TableController init]
- (id)init {
self = [super init];
if(self) {
UITabBarItem *tabBarItem = [[self navigationController] tabBarItem];
[tabBarItem setTitle:@"TWO"];
}
return self;
}
我设置第二个tabBarItem标题在正确的地方,显示,当我运行应用程序的第一个选项卡一,第二个是空白。
Am I setting the second tabBarItem title in the right place as currently it is not showing, when I run the application the first tab says "ONE" the second is blank. Also tabBarItem (above) shows nil when I print the value or look via the debugger.
EDIT_001:添加了来自AppDelegate的完整代码
当我第一次创建控制器时,我可以从AppDelegate中正确设置UITabBarItem,准备添加到UITabBarController。但是我真的想在个人控制器中使用这种方法 - 整洁的方法。
I can correctly set the UITabBarItem from within the AppDelegate when I first create the controllers, ready for adding to the UITabBarController. But I really wanted to do this in the individual controller -init methods for neatness.
// UITabBarController
UITabBarController *tempRoot = [[UITabBarController alloc] init];
[self setRootController:tempRoot];
[tempRoot release];
NSMutableArray *tabBarControllers = [[NSMutableArray alloc] init];
// UIViewController ONE
MapController *mapController = [[MapController alloc] init];
[tabBarControllers addObject:mapController];
[mapController release];
// UITableView TWO
TableController *rootTableController = [[TableController alloc] init];
UINavigationController *tempNavController = [[UINavigationController alloc] initWithRootViewController:rootTableController];
[rootTableController release];
[tabBarControllers addObject:tempNavController];
[tempNavController release];
[rootController setViewControllers:tabBarControllers];
[tabBarControllers release];
[window addSubview:[rootController view]];
[window makeKeyAndVisible];
EDIT_002:解决方案到目前为止,更新应用程序代理中的UITabBarItems,例如 p>
EDIT_002: Solution so far, Update UITabBarItems in app delegate e.g.
// TABLE CONTROLLER
TableController *tableController = [[TableController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tableController];
[tableController release];
[tabBarControllers addObject:navController];
UITabBarItem *tabBarItem_002 = [navController tabBarItem];
[tabBarItem_002 setTitle:@"TWO"];
[tabBarItem_002 setImage:[UIImage imageNamed:@"GT_TWO.png"]];
[navController release];
EDIT_003:
是有办法设置UITabBarItem
EDIT_003: is there a way to set the UITabBarItem in the UINavigationController or is the delegate (as it seems) really the best place to do it.
推荐答案
你真的要创建一个tabBarItem自己然后将它分配给视图控制器的tabBarItem属性。请尝试在您的init:
You actually have to create the tabBarItem yourself and then assign it to the view controller's tabBarItem property. Try this in your init:
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"TWO"
image:[UIImage imageNamed:@"GT_TWO.png"]
tag:0];
self.tabBarItem = tabBarItem;
[tabBarItem release];
希望这有帮助。
这篇关于从UINavigationController设置UITabBarItem标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!