iOS FAQ's Dynamic modification Uinavigationcontroller's Rightbarbuttonitem title

Source: Internet
Author: User

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

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.