开发者

Can't set UINavigationController title

So here's how I set up things in my app. I have a UINavigationController which has a UITabBarController as the rootViewController. The rootViewController has 3 tabs in it. I have tried many different ways to set the title, ranging from:

self.title = @"title";

self.navigationController.title = @"title";

self.navigationItem.title = @"title";

开发者_开发技巧

and none of it works

Why is this?

UPDATE:

for some weird reason self.parentViewController.title = @"Map"; actually worked....


I am guessing you want is to set the text that appears in the center of the navigation bar. If so, navigationItem is definitely not what you want. That's for setting buttons in the navigation bar.

The less obvious part of setting the title is that the text that will appear in the navigation bar is the title of the ViewController that is on top. In other words, the last ViewController that was pushed. So what you want to do is to set your self.title in your ViewContoller. You can do it ViewContoller's init or viewDidLoad.


I tried NSLogging every title attribute from the navigation bar, eventually for me it worked to set the navBar.topItem.title attribute. Of course, navBar is a reference to the UINavigationBar in my IB.


self.title should be what you use.

1.) When are you trying to change the title? for example, when pressing a tab button?

2.) Where are you placing this code? This should be in the viewDidLoad method


Here it comes....

You must write following line to add tabbarcontroller in appdelegate..

mTabBar = [[UITabBarController alloc] init];
    NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:3];


    TSDetailTaskController *mTSDetailTaskController = [[TSDetailTaskController alloc]initWithNibName:@"TSDetailTaskController" bundle:nil];
    UINavigationController *mTaskNavBar=[[UINavigationController alloc]initWithRootViewController:mTSDetailTaskController];
    mTaskNavBar.tabBarItem.title=@"Task List";
    mTaskNavBar.tabBarItem.image =[UIImage imageNamed:@"glyphicons_114_list.png"];
    [mTSDetailTaskController release];

    mTSSearchController=[[TSSearchController alloc]initWithNibName:@"TSSearchController" bundle:nil];
    UINavigationController *mSearchNavBar=[[UINavigationController alloc]initWithRootViewController:mTSSearchController];
    mSearchNavBar.title=@"Search";
    mSearchNavBar.tabBarItem.image=[UIImage imageNamed:@"glyphicons_009_search.png"];
    [mTSSearchController release];

    TSSettingController *mTSSettingController = [[TSSettingController alloc]initWithNibName:@"TSSettingController" bundle:nil];
    UINavigationController *mSettingNavBar=[[UINavigationController alloc]initWithRootViewController:mTSSettingController];
    mSettingNavBar.tabBarItem.title=@"Setting";
    mSettingNavBar.tabBarItem.image=[UIImage imageNamed:@"glyphicons_280_settings.png"];
    [mTSSettingController release];


    [localViewControllersArray addObject:mTaskNavBar];  
    [localViewControllersArray addObject:mSearchNavBar];
    [localViewControllersArray addObject:mSettingNavBar];

    [mTaskNavBar release];
    [mSearchNavBar release];
    [mSettingNavBar release];


    mTabBar.viewControllers = localViewControllersArray;
    mTabBar.view.autoresizingMask==(UIViewAutoresizingFlexibleHeight);

    [localViewControllersArray release];


    [window addSubview:mTabBar.view];
    [self.window makeKeyAndVisible];
    return YES;

I suppose that u r creating ur project based on UINavigation controller type...

then in the "ViewDidLoad" of ur rootviewcontroller try self.title

Hope this eill help u out..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜