开发者

Add titles to tabbar created programmatically

开发者 https://www.devze.com 2023-03-20 22:38 出处:网络
I am new to iPhone development...Now I am trying to build an application in which i am creating UITabbarController programmatically like below:

I am new to iPhone development... Now I am trying to build an application in which i am creating UITabbarController programmatically like below:

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

UINavigationController *tab开发者_运维知识库ItemOne = [[[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];

In this, how can I add titles to tabbar and those controllers. I have tried:

firstView.title = @"First View";

and

tabItemOne.title = @"First View";

But these two don't work.. How do I accomplish this?


Setting aViewController.title sets the title for both the navigationItem and the tabBarItem. If you want the navigationItem and the tabBarItem to have different title, do this,

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

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


To set the label for your controller's tab, do this:

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

(By the way, you may want to reconsider your variable names. They're pretty confusing. tabbar sounds like it should be an instance of the UITabBar, when it's actually a UITabBarController. firstView and secondView sound like instances of UIView when they're actually instances of a UIViewController subclass. tabItemOne and tabItemTwo sound like instances of UITabBarItem, when they're actually instances of UINavigationController.)


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";


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

If I'm correct.

0

精彩评论

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