ios UINavigationController 标题不显示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2040415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UINavigationController title is not displayed?
提问by senthilMuthu
I have normal UIViewController ,inwhich I added UINavigationControllerDelegate,i added as following in willappear?but it did not work?
我有普通的 UIViewController ,我在其中添加了 UINavigationControllerDelegate,我在 willappear 中添加了以下内容?但它不起作用?
[self.navigationController setNavigationBarHidden:NO animated:YES];
self.navigationController.navigationItem.title = @"hai";
回答by Henrik P. Hessel
You should set the title in the view controller you add to your navigation controller.
您应该在添加到导航控制器的视图控制器中设置标题。
i.e. in the add view controller
即在添加视图控制器中
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"My Title";
}
or access the array of your viewcontrollers directly
或直接访问您的视图控制器数组
AddFriendViewController *addFriendsView = [[AddFriendViewController alloc] initWithNibName:@"AddFriendViewController" bundle:nil];
[self.navigationController pushViewController:addFriendsView animated:YES];
[[self.navigationController.viewControllers objectAtIndex:0] setTitle:@"myTitle"];
[addFriendsView release];
回答by benzado
Every view controller has a navigation item. You are changing the navigation item of the navigation controller... but that will never be seen unless the navigation controller was inside another navigation controller! Instead of
每个视图控制器都有一个导航项。您正在更改导航控制器的导航项...但除非导航控制器位于另一个导航控制器内,否则永远不会看到!代替
self.navigationController.navigationItem.title = @"hai";
you want
你要
self.navigationItem.title = @"hai";
or, if your navigation item's title is nil, the navigation controller will use your view controller's title:
或者,如果您的导航项的标题为零,导航控制器将使用您的视图控制器的标题:
self.title = @"hai";
You should simply set the view title, which is used by both navigation bars and tab bars, unless you want to specify a different title for each of those for some reason.
您应该简单地设置导航栏和标签栏都使用的视图标题,除非您出于某种原因想为每个标题指定不同的标题。
回答by Henrik Erlandsson
If no viewcontroller has been pushed on the stack, ie. you're displaying the viewcontroller at the top of the stack (sometimes called rootViewController) you can do it with
如果没有视图控制器被推入堆栈,即。您在堆栈顶部显示视图控制器(有时称为 rootViewController),您可以使用
[rootViewController setTitle:@"Title"];
NOTE that you must use setTitle- rootViewController.title=@"Title" will normally not work.
请注意,您必须使用setTitle- rootViewController.title=@"Title" 通常不起作用。
Actually, you can do it with any viewcontroller, even if it's not viewDidLoad'ed yet. If it's created programmatically, just create it with
实际上,您可以使用任何视图控制器来执行此操作,即使它还没有被 viewDidLoad。如果它以编程方式创建,只需使用
myViewController *mvc=[[myViewController alloc] initWithNibName:@"nameofnibwithout.xib" bundle:nil];
Or you could declare it as an object in the app delegate, make it an IBOutlet, create a UIViewController and set its class to myViewController, and connect it to the IBOutlet so it's not nil.
或者您可以在应用程序委托中将其声明为一个对象,使其成为 IBOutlet,创建一个 UIViewController 并将其类设置为 myViewController,并将其连接到 IBOutlet 使其不为零。
回答by Jake Doty
You may need to set the Navigation bar to "Black Navigation Bar With Prompt" in the Attributes tab on the controller. That's what worked for me.
您可能需要在控制器的“属性”选项卡中将导航栏设置为“带提示的黑色导航栏”。这就是对我有用的东西。
回答by Denis Kutlubaev
In my case the title was not visible on iOS6
, when I set it up in the init
method:
在我的情况下,标题在 上不可见iOS6
,当我在init
方法中设置它时:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = LS(@"Favorites");
self.tabBarItem.title = LS(@"Favorites");
self.tabBarItem.image = [UIImage imageNamed:@"star"];
}
return self;
}
So, it should be moved from there to ViewDidLoad
.
所以,它应该从那里移到ViewDidLoad
.
回答by Adam Wo?
Are you sure that self.navigationController.navigationItem
is not nil
when you set its title?
你确定self.navigationController.navigationItem
不是nil
在你设置它的标题时?