1. Initialization
In Viewdidload, add a button named "Add Columns" for Navigationitem
-(void) viewdidload{ [Super viewdidload];//do any additional setup after loading the view. Self.navigationItem.rightBarButtonItem.title = @ "Add columns"; [Self.navigationItem.rightBarButtonItem Initwithbarbuttonsystemitem:uibarbuttonsystemitemundo target:self Action: @selector (Myaction)]; }
2. Attempt to change the button name failed attempts to modify the name of the button in the following manner, but failed
-(void) OnSelectionChanged: (ID) selection{ self.navigationItem.rightBarButtonItem.title = @ "edit"; }
3. Cause analysis
Why is the title of the Backbarbuttonitem set directly invalid?
View the Title Property description of the parent class uibaritem of the Apple document Uibarbuttonitem:
You should set before adding the item to a bar. The default value is nil. Therefore, the title cannot be modified, only the control itself can be reset.
4. Correct code 4.1 The idea of a single modification of the title is not, should I call the initialization method again? The code is as follows, eventually still no ah, no words
Self.navigationItem.rightBarButtonItem.title = @ "edit"; [Self.navigationItem.rightBarButtonItem initWithBarButtonSystemItem:UIBarButtonSystemItemUndotarget:selfaction: @selector (myaction)];
4.1 Thinking two since only the title does not work, then the whole button to replace it! Create a new button, replace Self.navigationItem.rightBarButtonItem with this new button, this time finally, thank goodness!!!
Uibarbuttonitem *temporarybarbuttonitem = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem: Uibarbuttonsystemitemundo target:self Action: @selector (Changewellcolumnaction)]; Temporarybarbuttonitem.title = @ "edit"; Self.navigationItem.rightBarButtonItem = Temporarybarbuttonitem; [Temporarybarbuttonitem release];
5. Summary of the problem in the idea of a failure of the reasons or not to find, thinking two have no side-effect is not very clear, welcome to the great God of all the way to enlighten!!!
iOS FAQ's Dynamic modification Uinavigationcontroller's Rightbarbuttonitem title