0

I want a UINavigationBar whose contents change in response to other events in the app. Most immediately, I'd like to have the buttons on the bar loaded dynamically in response to other variables in the program. But in the more general case I'd like to be able to change the contents of the UINavigationBar on the fly, while the program is running.

The hurdle I'm running into is that the UINavigationBar loads its contents when its first displayed and there doesn't seem to be a method to make it alter them after that. What's the best workaround?

baudot
  • 1,618
  • 20
  • 33

2 Answers2

1

This is fairly easy to do. The best way is to pre-load all of the options for the different objects you want in your navBar so that you need only switch them in and out based on the user's input, but you can load them on the fly. When the user does an action which you want to change the navBar, simply add:

self.navigationItem.rightBarButtonItem = nil;
UIBarButtonItem * newButton = [[UIBarButtonItem alloc] initWithTitle:@"What you want to call it" style:UIBarButtonItemStyleBordered target:self action:@selector(whatYouWantTheButtonToCall)];
self.navigationItem.rightBarButtonItem = newButton;
[newButton release];

For other options then a button, you can look here. Hope that helps!

msgambel
  • 7,320
  • 4
  • 46
  • 62
  • What you've just described is exactly what fails. As stated in the write-up, once the view is loaded, simply changing the button property doesn't change the view. – baudot Aug 11 '11 at 17:21
  • By the contents of the navBar, you you mean the navBar itself, or the current view displayed under the navBar? If it's the former, then the above code works. – msgambel Aug 11 '11 at 17:42
  • Changing the NavBar itself. I've tested the snippet provided, and it doesn't work. We're talking about dynamically changing the view AFTER the screen has finished loading. The whole problem is that changes to the properties don't appear. – baudot Aug 11 '11 at 20:59
  • The answer turns out to be running [self loadView] on the viewController after you make the needed change. – baudot Aug 11 '11 at 20:59
  • That's not true. This works AFTER the screen has been loaded. Hook it up to a button and test it out. – msgambel Aug 12 '11 at 02:36
  • I did. It didn't work. Maybe it works for you. Maybe there's something different about the set-up of your app. But I tried it, like I already said, and it didn't work on my end. [self loadView] did. – baudot Aug 12 '11 at 05:58
  • I mean, seriously. That's the FIRST THING I TRIED. When you wrote it in, I thought maybe I'd missed some little thing, so I copied your code to test and see. It still didn't work. – baudot Aug 12 '11 at 06:02
0

OK, I found one way: After editing a navigation bar item, you can call [self loadView] ...to prompt a reload.

That works, but it's a bit blech.

baudot
  • 1,618
  • 20
  • 33