How to change type of UIBarButtonItem to UINaviagationBar at runtime?

I am working on an iPhone view that consists of three elements: UITextView, UIToolBar with UIBarButtonItem.

The goal is for the UIBarButtonItem to change its style from "edit" (UIBarButtonSystemItemEdit) to Finish (UIBarButtonSystemItemDone) and update the new selector to the new method.

First of all, I tried the following code, but it does not work:

Could you help me with this idea?

+7
iphone uinavigationbar uibarbuttonitem
source share
4 answers

There is a built-in button with this behavior, you get it through the editButtonItem UIViewContoller property. By clicking this button, you will change the view controller from which it went into edit mode, and put the button in the button that was pressed.

 - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.rightBarButtonItem = self.editButtonItem; } 
+14
source share

If you added the button via IB, make sure that this identifier is set to Custom. Also select the button in .h using the corresponding IBOutlet and Property Synthesize the button in .m

Then in your code do the following:

 // Set to done editButton.style = UIBarButtonItemStyleDone; editButton.title = @"Done"; // Set back to edit editButton.style = UIBarButtonItemStyleBordered; editButton.title = @"Edit"; 
+5
source share

to change the finish button use this

 [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; 

to change the button to the "Change" button, use this

 [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleBordered]; 
+4
source share

I ended up doing something like this. Unfortunately, setting the header did not work directly, for some reason it was zero and did not allow me to set it to a different value. Self.editButton comes from IBOutlet with a goal and a set of actions. This code uses ARC. I hope this helps someone.

  NSString *title = app.settings.editing ? NSLocalizedString(@"Done", @"") : NSLocalizedString(@"Edit", @""); UIBarButtonItemStyle style = app.settings.editing ? UIBarButtonItemStyleDone : UIBarButtonItemStyleBordered; UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:title style:style target:self.editButton.target action:self.editButton.action]; self.navigationItem.rightBarButtonItem = editButton; 
+2
source share

All Articles