UINavigationController标题不显示?

sen*_*thu 20 objective-c uinavigationcontroller ios

我有正常的UIViewController,其中我添加了UINavigationControllerDelegate,我在willappear中添加如下?但它不起作用?

    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationController.navigationItem.title = @"hai";
Run Code Online (Sandbox Code Playgroud)

Hen*_*sel 44

您应该在添加到导航控制器的视图控制器中设置标题.

即在添加视图控制器中

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"My Title";
}
Run Code Online (Sandbox Code Playgroud)

或直接访问viewcontrollers数组

        AddFriendViewController *addFriendsView = [[AddFriendViewController alloc] initWithNibName:@"AddFriendViewController" bundle:nil];
        [self.navigationController pushViewController:addFriendsView animated:YES];
        [[self.navigationController.viewControllers objectAtIndex:0] setTitle:@"myTitle"];
        [addFriendsView release];
Run Code Online (Sandbox Code Playgroud)


ben*_*ado 16

每个视图控制器都有一个导航项.您正在更改导航控制器的导航项...但除非导航控制器位于另一个导航控制器内,否则永远不会看到它!代替

self.navigationController.navigationItem.title = @"hai";
Run Code Online (Sandbox Code Playgroud)

你要

self.navigationItem.title = @"hai";
Run Code Online (Sandbox Code Playgroud)

或者,如果导航项目的标题为零,导航控制器将使用您的视图控制器的标题:

self.title = @"hai";
Run Code Online (Sandbox Code Playgroud)

您应该只设置视图标题,导航栏和标签栏都使用它,除非您因某些原因要为每个标题指定不同的标题.