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];