Pamy
Pamy

Reputation: 193

Button not being added on navigation controller

- (void)viewDidLoad
{
    [super viewDidLoad];

    ac = [[AddContacts alloc]init];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:ac];
    [self.view addSubview:navigationController.view];

    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];          
    self.navigationController.navigationItem.rightBarButtonItem = anotherButton;
}

Why is my button not being added on the navigation controller?

Upvotes: 0

Views: 118

Answers (2)

Simon Goldeen
Simon Goldeen

Reputation: 9080

There are a few things here that could be causing issues.

Probably what the main issue is this line:

self.navigationController.navigationItem.rightBarButtonItem = anotherButton;

I am pretty sure that what you want to be setting is the right bar button item on the ac view controller's navigation item.

ac.navigationItem.rightBarButtonItem = anotherButton

A few other things though:

  • Don't have two letter variable names. "ac" is very ambiguous, "addContacts" would provide more information, "addContactsViewController" would provide even more
  • Are you implementing your own navigationController property in a UIViewController subclass? This is not recommended as it is overriding the navigationController property that UIViewController already has. Give it a different name.
  • Is the -viewDidLoad method on the parent view controller the place to be assigning the right bar button of your AddContacts object? Consider instead putting the code to set the bar button in the implementation of AddContacts instead.

Upvotes: 1

Evan Mulawski
Evan Mulawski

Reputation: 55334

The UIBarButtonItems are not controlled by the navigation controller, but by each of the view controllers it contains - each UIViewController can have different buttons. Try:

ac = [[AddContacts alloc]init];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];          
ac.navigationItem.rightBarButtonItem = anotherButton;

Then initialize the UINavigationController as you have been doing.

Upvotes: 1

Related Questions