I'm trying to add button to navigation bar i want to add the button without any action any help please?
4 Answers
UIBarButtonItem *yourButton = [[UIBarButtonItem alloc]
initWithTitle:@"Your Button"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(methodName:)];
self.navigationItem.rightBarButtonItem = yourButton;
[yourButton release];
And then method
-(IBAction)methodName {
// your code here
}
Comments
You should add UIBarButtonItem to Nav bar instead of UIButton. T do that you can call this:
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithTitle:@"Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.rightBarButtonItem = button;
UIBarButtonItem *customBtn=[[UIBarButtonItem alloc] initWithTitle:@"Custom" style:UIBarButtonItemStylePlain target:self action:@selector(customBtnPressed)];
[self.navigationItem setRightBarButtonItem:customBtn];
///// called event
-(IBAction)customBtnPressed:(id)sender
{
//Your code here
}
Comments
if UIButton is you really want, try this.
UIButton *btnSample = [[UIButton alloc] initWithFrame:btnFrame];
btnSample.backgroundColor = [UIColor blueColor];
UIBarButtonItem *barBtn_cart = [[UIBarButtonItem alloc] initWithCustomView:btnSample];
self.navigationItem.rightBarButtonItem = btnSample;