Adding BarButtonItem To NavigationBar
I Added a NavigationBar Through IB and I Tried to Add a BarButtonItem Programatically.....But it Doesn't Work.
- (void)viewDidLoad {
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit"style:UIBarB开发者_开发知识库uttonItemStyleBordered target:self action:@selector(EditTable:)];
[self.navigationItem setLeftBarButtonItem:self.addButton];
[super viewDidLoad];
}
Try to do this :
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTapped:)];
In your case try this:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit"style:UIBarButtonItemStyleBordered target:self action:@selector(EditTable:)];
[self.navigationItem setLeftBarButtonItem:addButton];
I guess this might work.
What you are doing is right, but so far, you've only added the addButton to a UINavigationItem as the leftBarButtonItem. To add it to a navigationBar, you need to then push the navigationItem onto it, similar to how you add a new UIViewController to a UINavigationController. Assuming you name your UINavigationBar navBar:
UINavigationItem *navigationItem = [[UINavigationItem alloc] init];
self.addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit"style:UIBarButtonItemStyleBordered target:self action:@selector(EditTable:)];
[navigationItem setLeftBarButtonItem:self.addButton];
[self.navBar pushNavigationItem:navigationItem animated:NO];
[navigationItem release];
Hope that helps!
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] init];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Edit"style:UIBarButtonItemStyleBordered target:self action:@selector(EditTable:)];
self.navigationItem.leftBarButtonItem = addButton;
self.navigationItem.leftBarButtonItem.enabled = YES;
For some views the leftBarButtonItem doesnt work and you need to use the backBarButtonItem. Try this:
- (void)viewDidLoad {
[self.navigationItem setBackBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(EditTable:)] autorelease];
// Or, if that doesnt work for some reason use:
// [self.navigationItem setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(EditTable:)] autorelease];
[super viewDidLoad];
}
Also, your creating a variable within the scope of viewDidLoad
called addButton
, but then you are trying to set the property self.addButton
as the leftBarButtonItem
.
Either remove the self.
from the self.addButton
in:
[self.navigationItem setLeftBarButtonItem:self.addButton];
or initialize the property instead of creating a new item with the same name:
self.addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit"style:UIBarButtonItemStyleBordered target:self action:@selector(EditTable:)];
// instead of:
// UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit"style:UIBarButtonItemStyleBordered target:self action:@selector(EditTable:)];
精彩评论