UITabBarController中的UINavigationController,设置导航控制器标题

时间:2022-04-05 20:38:39

In my app I have a UINavigationController within a UITabBarController. Everything is working fine, however I can't set the title for the navigation controller. I have tried several different methods and googled around but there seems to be no solution for this problem.

在我的app中有一个UINavigationController在UITabBarController中。一切正常,但是我不能为导航控制器设置标题。我尝试了几种不同的方法,在谷歌上搜索了一下,但是似乎没有解决这个问题的办法。

Any Help would be greatly appreciated.

如有任何帮助,我们将不胜感激。

Thankyou in advance.

提前谢谢。

Sam

山姆

5 个解决方案

#1


38  

I found the answer in the end, it was:

最后我找到了答案:

self.tabBarController.navigationItem.title = @"title";

#2


2  

self.navigationItem.title = @"Your Title"

Works for every case.

适用于所有情况。

#3


1  

For me the following works fine:

对我来说,以下方法很有效:

Initiate the controllers in appDelegateDidFinishLaunching:Method:

初始化appDelegateDidFinishLaunching中的控制器:方法:

UINavigationController *navContr1;
UINavigationController *navContr2;

UIViewController *viewController1, *viewController2;

viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil] autorelease];



navContr1       = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
navContr2       = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];


self.tabBarController = [[[UITabBarController alloc] init] autorelease];
//self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navContr1, navContr2, nil];

self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

with this done, in your different viewControllers initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil - Method you can change the title with the following line:

这样做之后,在不同的viewcontrollerinitwithnibname:(NSString *)nibNameOrNil:(NSBundle *)nibBundleOrNil -方法中,您可以用以下代码更改标题:

self.title = @"Your Title";

Good Luck.

祝你好运。

#4


0  

Look at the UIViewController class in the docs. There is a property pointing to the UINavigationItem which then has a property pointing to the title. If the pointer to the navigation item returns nil, you must have not wired the controllers together correctly.

看看文档中的UIViewController类。有一个属性指向UINavigationItem,然后有一个属性指向标题。如果指向导航项的指针返回nil,则必须没有正确地将控制器连接在一起。

#5


0  

The self.title always sets the tile of the rootViewController object of your ViewController.

自我。title总是设置视图控制器的rootViewController对象的块。

If you have your viewController & the rootViewController is UINavigationController, then self.title will set the title of UINavigationController.

如果你有你的viewController和rootViewController是UINavigationController,然后是self。title将设置UINavigationController的标题。

Similarly, if you have initialized a UITabViewController with the UIViewController objects, then the self.title will applies on the title corresponding button at index, on UITabBar of UITabViewController.

类似地,如果你用UIViewController对象初始化了一个UITabViewController,然后是self。title将应用于UITabViewController的UITabBar上索引处的标题对应按钮。

Hence said that, self.title applies on the object holding the viewController.

因此说,自我。title适用于持有viewController的对象。

#1


38  

I found the answer in the end, it was:

最后我找到了答案:

self.tabBarController.navigationItem.title = @"title";

#2


2  

self.navigationItem.title = @"Your Title"

Works for every case.

适用于所有情况。

#3


1  

For me the following works fine:

对我来说,以下方法很有效:

Initiate the controllers in appDelegateDidFinishLaunching:Method:

初始化appDelegateDidFinishLaunching中的控制器:方法:

UINavigationController *navContr1;
UINavigationController *navContr2;

UIViewController *viewController1, *viewController2;

viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil] autorelease];



navContr1       = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
navContr2       = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];


self.tabBarController = [[[UITabBarController alloc] init] autorelease];
//self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navContr1, navContr2, nil];

self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

with this done, in your different viewControllers initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil - Method you can change the title with the following line:

这样做之后,在不同的viewcontrollerinitwithnibname:(NSString *)nibNameOrNil:(NSBundle *)nibBundleOrNil -方法中,您可以用以下代码更改标题:

self.title = @"Your Title";

Good Luck.

祝你好运。

#4


0  

Look at the UIViewController class in the docs. There is a property pointing to the UINavigationItem which then has a property pointing to the title. If the pointer to the navigation item returns nil, you must have not wired the controllers together correctly.

看看文档中的UIViewController类。有一个属性指向UINavigationItem,然后有一个属性指向标题。如果指向导航项的指针返回nil,则必须没有正确地将控制器连接在一起。

#5


0  

The self.title always sets the tile of the rootViewController object of your ViewController.

自我。title总是设置视图控制器的rootViewController对象的块。

If you have your viewController & the rootViewController is UINavigationController, then self.title will set the title of UINavigationController.

如果你有你的viewController和rootViewController是UINavigationController,然后是self。title将设置UINavigationController的标题。

Similarly, if you have initialized a UITabViewController with the UIViewController objects, then the self.title will applies on the title corresponding button at index, on UITabBar of UITabViewController.

类似地,如果你用UIViewController对象初始化了一个UITabViewController,然后是self。title将应用于UITabViewController的UITabBar上索引处的标题对应按钮。

Hence said that, self.title applies on the object holding the viewController.

因此说,自我。title适用于持有viewController的对象。