Search code examples
iphoneuinavigationbarmodal-view

What is the proper way to access navbar buttons of a modal view?


I created a view in IB with a navbar and a table. On the navbar I put two buttons, cancel and done. I use this view like a modal view with:

[self presentModalViewController:controller animated:YES];

My problem is when I use:

[self.navigationItem.rightBarButtonItem setEnabled:YES];

to enable the right button. It doesn't work.

Have I to set a delegate? what code passages I have to do? It works if I create an IBOutlet for the right button and I use [doneButton setEnabled:YES], but I think this isn't the proper way.


Solution

  • Delegate has nothing to do with your issue.

    You probably did put navigation bar into your view directly. Thus things like self.navigationItem doesn't work. You have two choices ...

    Connect your buttons to outlets in your code and access them directly.

    Or remove navigation bar from your view and present your view controller in this way ...

    MyViewController *vc = [[MyViewController alloc] initWith...];
    UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:vc];
    [vc release];
    [self presentModalViewController:navCon animated:YES];
    [navCon release];
    

    ... and now you can access left/right buttons via navigationItem.