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?

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

7 Comments

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.
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.
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.
The answer turns out to be running [self loadView] on the viewController after you make the needed change.
That's not true. This works AFTER the screen has been loaded. Hook it up to a button and test it out.
|
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.

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.