将标题添加到以编程方式创建的标签栏

时间:2011-07-15 04:41:35

标签: iphone objective-c ios uitabbarcontroller tabbarcontroller

我是iPhone开发的新手......现在我正在尝试构建一个应用程序,我在其中以编程方式创建UITabbarController,如下所示:

UITabBarController *tabbar = [[UITabBarController alloc] init];
firstViewController  *firstView = [[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];

UINavigationController *tabItemOne = [[[UINavigationController alloc] initWithRootViewController: firstView] autorelease];
secondViewController *secondView = [[secondViewController alloc] initWithNibName:@"secondViewController" bundle:nil];

UINavigationController *tabItemTwo = [[[UINavigationController alloc] initWithRootViewController: settings] autorelease];
tabbar.viewControllers = [NSArray arrayWithObjects:tabItemOne, tabItemTwo,nil]; 
tabbar.view.frame = CGRectMake(0,0,320,460);

[self.view insertSubview:tabbar.view belowSubview: firstView.view];
[self presentModalViewController:tabbar animated:NO];

在此,我如何向tabbar和那些控制器添加标题。 我试过了:

firstView.title = @"First View";

tabItemOne.title = @"First View";

但这两个不起作用..我该如何做到这一点?

4 个答案:

答案 0 :(得分:3)

设置 aViewController.title navigationItem tabBarItem 设置标题。如果您希望 navigationItem tabBarItem 具有不同的标题,请执行此操作,

// First set the view controller's title
aViewController.title = @"First View Tab";

// Then set navigationItem's title
aViewController.navigationItem.title = @"First View";

答案 1 :(得分:0)

UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"First View" image:[UIImage imageNamed:@"First View.png"] tag:0];
[tabItemOne setTabBarItem:item];
[item release];

如果我是对的。

答案 2 :(得分:0)

要设置控制器选项卡的标签,请执行以下操作:

tabItemOne.tabBarItem.title = @"First View";

(顺便说一下,你可能想重新考虑一下你的变量名。它们很混乱。tabbar听起来应该是UITabBar的一个实例,当它实际上是一个UITabBarController时。的firstView和secondView声音就像UIView的实例一样它们实际上是UIViewController子类的实例.tabItemOne和tabItemTwo听起来像UITabBarItem的实例,当它们实际上是UINavigationController的实例时。)

答案 3 :(得分:0)

UITabBarItem *item= [[[[appDelegate tabBarController]tabBar]items ]objectAtIndex:0];
       //item.image=[UIImage imageNamed:@"hometab.png"];
       item.title=@"First View";

    item= [[[[appDelegate tabBarController]tabBar]items ]objectAtIndex:1];
       //item.image=[UIImage imageNamed:@"hometab.png"];
       item.title=@"Second View";