0

I am using a UINavigationController and setting the toolbar items by using the method setToolbarItems of my topmost UIViewController. This works fine the first time when the user goes to the screen or when the user revisits the screen. However I also have a background thread which communicates with the server. Server send the message to remove some button from the toolbar or change the icon due to the application logic. At that time if I call setToolbarItems method of my current UIViewController on the main thread, the toolbar items are not updated. So what is the way to reload the UIToolbar of the UINavigationController without reloading the entire view.

Thanks

user2217812
  • 37
  • 1
  • 6

1 Answers1

0

I had a similar problem. When the user leaves my app to change the color scheme in ‘Settings’, the navigation bar, text, and color attributes needed to be updated. I was able to update the nav bar color but the nav bar text color was not getting updated. My solution: I am not using ‘Storyboards’. In my AppDelegate.h I have:

@property (….) CustomViewController *viewController;
@property (….) UINavigationController *nav; 

I did not @synthesize in AppDelegate.m. Then in AppDidFinishLaunchingWithOptions:

….
self.viewController  = [[CustomViewController alloc ….initWithNib….
[self setNavControllerAttributes];  // My solution is in this method
self.window.rootViewController = self.nav;
….

The method that does most of the work is in AppDelegate.m

- (void) setNavControllerAttributes
{
    self.nav = nil;
    self.nav = [[UINavigationController alloc…
    // Set my attributes with dictionary options and such
    self.window.rootViewController = self.nav;
}

I know I am setting the *nav to nil even the first time the app runs but it works and it will be also set to nil when the user leaves and relaunches the app. My only concern would be the fact that I am reassigning the rootViewController to window, but I think ARC will take care of my memory management. To make this work I call the method again on re-launch in my applicationWillEnterForeground:

[self setNavControllerAttributes];

I’m sure you can tweak it to your needs. The only other thing to watch out for, I guess, is saving your data because when you set *nav to nil, you may deallocate the child controllers and lose data. This works for iOS 6. If I notice that a different approach is needed for iOS 7, I will come back and update. If anyone has additional suggestions I will take the advice. I hope this helps

In applicationWillEnterForeground: you can even do something better to minimize memory and performance overhead. Currently this is my method to also update the first view controller

if (self.viewController.isViewLoaded) {
    [self setNavControllerAttributes];
    [self.viewController viewWillAppear:YES];
}
if (self.viewController.view.window) {
    // Perform other action if code above doesn't work
}
Murat Zazi
  • 347
  • 4
  • 10