Mads Mob?k: Example code to add a button to Uinavigationbar
| 12345678 |
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:nil action:nil];UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];item.rightBarButtonItem = rightButton;item.hidesBackButton = YES;[bar pushNavigationItem:item animated:NO];[rightButton release];[item release]; |
But usually you have to have a navigation Controller that allows you to write the following code:
| 1234 |
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:nil action:nil];self.navigationItem.rightBarButtonItem = rightButton;[rightButton release]; |
Amagrammer: The answer upstairs is very good, and I would like to make some suggestions:
If you modify the title of the Back button, you must modify it in the previous view controller instead of where it will be displayed. It's like saying, "Hey, if you've ever placed another view controller on this, then call the Back button instead of default."
If you want to hide the back button in a particular state, such as when displaying uipickerview, use Self.navigationItem.hidesBackButton = YES; When you exit this state, remember to set it back.
If you want to display a special symbolic button, use the InitWithBarButtonSystemItem:target:action method with controls like Uibarbuttonsystemitemadd.
Remember, the meaning of the symbol is up to you, but be careful with the HMI interaction Guide. Using uibarbuttonsystemitemadd means deleting an item will likely cause your app to be rejected.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
How do I add a button to Uinavigationbar?