Programmatically Changing UIBarButtonItem Identifier Property

Through Interface Builder, I have the opportunity to change the UIBarButtonItem identifier to something like "Add" (or "Cancel", "Repeat", etc.). This gives my button a nice “+” image.

How can I install this programmatically? UIBarButtonItem does not accept the message "setIdentifier".

+5
source share
3 answers

Once created, the UIBarButtonItem identifier cannot be changed. However, the user interface can be changed by replacing the button with a software-built option. For example:

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                                                                           target:self 
                                                                           action:@selector(doAddAction:)];
+6
source

: bordered identifier:

UIBarButtonItem *btn;
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(shotAction:)];
btn.style=UIBarButtonItemStyleBordered;
+2

After creating a UIBarButtonItem, you cannot change the identifier. However, you can create a new UIBarButtonItem to replace the old UIBarButtonItem

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                                              target:self
                                                              action:@selector(buttonClickedAction:)];

self.navigationItem.rightBarButtonItem = barButton;
0
source

All Articles