1110
1110

Reputation: 6829

Can't add button on navigation bar

I have UItable with items. I have navigation bar etc. On one item in table "Category" I pust another UITable like this:

CategoryTableViewController *b = [[CategoryTableViewController alloc]init];
        [self.navigationController pushViewController:b animated:YES];
        [b release];

Now I want to add "ADD" button in navigation bar and I add UINavigationBarItem in *.xib connect it to outlets and add it like this in viewDidLoad:

self.navigationItem.rightBarButtonItem = self.addButton;

And this does not work (addButton is null), but when I put the same code for adding button in my first UITable it works fine and "ADD" button is added.

What could be the problem here?

Upvotes: 1

Views: 2893

Answers (3)

Rene Berlin
Rene Berlin

Reputation: 1171

In the ViewController that should show the button in the navigation bar type in the viewDidLoad() method:

self.addToolbarButton = [[[UIBarButtonItem alloc]           
initWithTitle:@"Add", nil)
    style:UIBarButtonSystemItemAdd 
    target:self
    action:@selector (add)] autorelease];
self.navigationItem.leftBarButtonItem = addToolbarButton;

That will add a "add" styled button to the left in the navigation bar which calls the selector method:

-(void) add {...}

in the same class when it is tapped. In this method in the same class you can specify your add logic. If this method should be placed in a different class, set the target to that.

That is the programatical way to solve this. The method "-(void) add" is what your Outlet has been in the .xib approach.

For your .xib approach you should verify that the Outlet property for the navigationBarButton is set to retain.

Upvotes: 2

Antwan van Houdt
Antwan van Houdt

Reputation: 6991

self.addButton is NULL, thus make sure its not NULL. Create a button from code.

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneMeasuring:)];

Upvotes: 1

Matthias Bauch
Matthias Bauch

Reputation: 90117

is [super viewDidLoad]; the first call in your viewDidLoad method?

If not try to put it at the very beginning of viewDidLoad.

Upvotes: 0

Related Questions