Objective c - how to display a shorter name of viewController on back button

折月煮酒 提交于 2019-12-24 08:35:13

问题


I almost sure I saw someday that there is some property to set a shorter name of viewController to be display in the back button when it push another viewController and become the back viewController. Can some one remind me what is this property?


回答1:


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    self.title = @"First View Controller";

    // this defines the back button leading BACK TO THIS controller
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc]
      initWithTitle:@"Back"
      style:UIBarButtonItemStyleBordered
      target:nil
      action:nil];
    self.navigationItem.backBarButtonItem = backBarButtonItem;
    [backBarButtonItem release];
    }
    return self;
}



回答2:


self.navigationItem.title = @"short name";




回答3:


You need to customize the backBarButtonItem from the navigationItem in your parent view controller.




回答4:


I'm curious to see what the "right" answer is. Personally I change the name of the last controller (like the root), which works.

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [super navigationController:navigationController willShowViewController:viewController animated:animated];
    UIViewController *root = [self.navigationController.viewControllers objectAtIndex:0];
    if (something)
        root.title = @"Apply";
    else
        root.title = "Root"
}

This works identically to self.navigationItem.title in all regards.



来源:https://stackoverflow.com/questions/10282345/objective-c-how-to-display-a-shorter-name-of-viewcontroller-on-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!