Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change UIBarButtonItem's type in UINaviagationBar at runtime?

I am working on an iPhone's view which composed 3 elements, UITextView, UIToolBar with an UIBarButtonItem.

The goal is, I want UIBarButtonItem change its style from 'edit' (UIBarButtonSystemItemEdit) to 'Done' (UIBarButtonSystemItemDone) and update new selector to new method.

First of all, I have tried following code but it doesn't work:

Could you help me on this idea?

like image 859
Teerasej Avatar asked Oct 30 '09 06:10

Teerasej


3 Answers

There is a builtin bar button with this behaviour, you get it via the editButtonItem property of a UIViewContoller. Tabbing that button will change the view controller it came from into editing mode, and toggle the button into a done button.

- (void)viewDidLoad {
  [super viewDidLoad];
  self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
like image 112
Louis Gerbarg Avatar answered Nov 03 '22 18:11

Louis Gerbarg


If you have added the button through IB then make sure to set the identifier to Custom Also allocate a button in the .h with appropriate 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";
like image 23
GregLBS Avatar answered Nov 03 '22 18:11

GregLBS


to change the button the Done button use this

[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];

to change the button to Edit button use this

[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleBordered];
like image 4
Saurabh Avatar answered Nov 03 '22 18:11

Saurabh