2

I'm trying to add button to navigation bar i want to add the button without any action any help please?

1

4 Answers 4

3
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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;

2 Comments

@gerg I'm using xib not storyboard
It doesn't matter, it will work for both. Maybe it will require you to create outlet from nav bar. If you use xib why do you want to add this button programatically? you can do it in IB.
0
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

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.