Problems adding a UIBarButtonItem programmatically or editing one inserted via IB
I'm working on project that uses a tab bar at the bottom to flip through 5 sections of my app. One of these sections loads a map view which then spawns a web view. This was done with Round Rect Buttons at first, but they got in the way of the views, so I decided to switch to a navigation bar. In Interface Builder I inserted a navigation item and within it two Bar Button Items (left and right side). I've gotten the functionality for both button to work by control dragging from file's owner to fire the method associated with each button. This works fine and launches my web view, but what I would like to be able to do and have yet to figure out how to so far, is changing one of the buttons to a "done" or "back" button once in the web view. I've tried creating both buttons programmatically but neither would appear. This is the code I tried using to accomplish this...
UIBarButtonItem *updatePosition = [[UIBarButtonItem alloc] initWithTitle:@"Update Position"
style:UIBarButtonItemStylePlain target:self action:@selector(findMe)];
self.navigationItem.leftBarButtonItem = updatePosition;
[updatePosition release];
When I run the app, the button does not show up on the navigation item I inserted using Xcode. The only way I can get the buttons to appear is by inserting them with Interface Builder as I previously mentioned. But then I can not change the left button to a "back" or "done" button once I've brought up the new view. I tried running this code to change the style of the button on the UIBarButtonItem I inserted using IB with this code
self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleDone;
But to no a开发者_运维知识库vail.
Here are some Utility methods to help you along the way:
+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withImage:(NSString *)imageName {
UIImage *buttonImage = [UIImage imageNamed:imageName];
UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithImage:buttonImage
style:UIBarButtonItemStyleBordered
target:sender
action:action] autorelease];
return buttonItem;
}
+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withBarButtonSystemItem:(UIBarButtonSystemItem)systemItem {
UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem
target:sender
action:action] autorelease];
return buttonItem;
}
Usage:
// Done button.
self.navigationItem.rightBarButtonItem = [Utility setRightBarButtonItem:@selector(doneTapped:) target:self withBarButtonSystemItem:UIBarButtonSystemItemDone];
精彩评论