Iphone 如何以编程方式设置导航栏的标题?

Iphone 如何以编程方式设置导航栏的标题?,iphone,Iphone,如何以编程方式设置导航栏的标题 self.title = @"My View"; 或 取决于您是否正在使用UINavigationController。在视图控制器实现文件中: - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

如何以编程方式设置导航栏的标题

self.title = @"My View";


取决于您是否正在使用
UINavigationController

在视图控制器实现文件中:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
     if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
             self.title = @"My Title";
     }
     return self;
}

这应该有效。

UINavigationBar中显示的标题来自当前活动的UIViewController

例如,假设我们想要一个UINavigationController,它有一个名为MyCustomViewController的根视图控制器

在我们的应用程序代理中:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    UIViewController *myCustomViewController = [[MyCustomViewController alloc] init];
    navController = [[UINavigationController alloc] initWithRootViewController:myCustomViewController];
    [window addSubview:navController.view];
    [window makeKeyAndVisible];
}
在MyCustomViewController.m中:

- (void)viewDidLoad {
    self.title = @"Hello World!";
}

我能够成功地在viewcontroller推送上设置标题。或者更确切地说,在推之前。这是我的代码

    _chatTableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChatTableViewController"];
    _chatTableViewController.title = @"Live Chat";
    [[self navigationController]pushViewController:_chatTableViewController animated:YES];

因为我没有使用UINavigationController,所以在创建了一个IBOutlet到navigationBar之后,我使用了第二种语法。非常感谢。
    _chatTableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChatTableViewController"];
    _chatTableViewController.title = @"Live Chat";
    [[self navigationController]pushViewController:_chatTableViewController animated:YES];