UINavigationController “back button” custom text?

后端 未结 16 1452
Happy的楠姐
Happy的楠姐 2020-11-28 01:41

The \"back button\" of a UINavigationController by default shows the title of the last view in the stack. Is there a way to have custom text in the back button

16条回答
  •  無奈伤痛
    2020-11-28 02:29

    Expanding on Aubrey's suggestion, you can do this in the child view controller:

    create two variables for storing the old values of the parent's navigationItem.title and the parent's navigationItem

    UINavigationItem* oldItem;
    NSString* oldTitle;
    

    in viewDidLoad, add the following:

    oldItem = self.navigationController.navigationBar.topItem;  
    oldTitle = oldItem.title;  
    [oldItem setTitle: @"Back"];  
    

    in viewWillDisappear, add the following:

    [oldItem setTitle: oldTitle];  
    oldTitle = nil;  // do this if you have retained oldTitle
    oldItem = nil;   // do this if you have retained oldItem
    

    It's not perfect. You will see the the title of the parent view change as the new controller is animated in. BUT this does achieve the goal of custom labeling the back button and keeping it shaped like a standard back button.

提交回复
热议问题