Modify the title of rightBarButtonItem of UINavigationController

Source: Internet
Author: User

Modify the title of rightBarButtonItem of UINavigationController
1. Initialization

In viewDidLoad, add the button named "add partition" for navigationItem

 

-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. self. navigationItem. rightBarButtonItem. title = @ Add a column; [self. navigationItem. rightBarButtonItem initWithBarButtonSystemItem: UIBarButtonSystemItemUndo target: self action: @ selector (myAction)];}

 

 

 

2. An error occurred while trying to change the button name.

 

-(Void) onSelectionChanged :( id) selection {self. navigationItem. rightBarButtonItem. title = @ edit ;}


 

3. Cause Analysis

Why is setting the title of backBarButtonItem invalid?

View the title attribute description of UIBarItem, the parent class of UIBarItem in the Apple documentation:

You shoshould set this property before adding the item to a bar. The default value is nil. Therefore, the title cannot be modified, and The control itself can only be reset.

 

4. correct code 4.1 The idea is that it is not feasible to modify the title. Should I call the initialization method again? The Code is as follows. It will not work in the end. It's speechless.
Self. navigationItem. rightBarButtonItem. title = @ edit; [self. navigationItem. rightBarButtonItem initWithBarButtonSystemItem: UIBarButtonSystemItemUndotarget: selfaction: @ selector (myAction)];


4.1 train of thought 2 since only modifying the title does not work, replace the entire button! Create a new button and use this button to replace self. navigationItem. rightBarButtonItem. This time, it's okay. Thank God !!!
UIBarButtonItem * temporaryBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: Custom target: self action: @ selector (changeWellColumnAction)]; temporaryarybarbuttonitem. title = @ edit; self. navigationItem. rightBarButtonItem = temporaryBarButtonItem; [temporaryBarButtonItem release];

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.