Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The accepted answer works, but it breaks the contract on UIViewController, your child controllers are now tightly coupled with your custom category and must use its alternative methods in order to work correctly... I had this issue using the RBStoryboardLink container, and also on a custom tab bar controller of my own, so it was important it would be encapsulated outside of a given container class, so I created a class that has a mirrorVC property (usually set to the container, the one who will listen for notifications) and a few register / unregister methods (for navigationItems, toolbarItems, tabBarItems, as your needs see fit). For example when registering/unregistering for toolbarItems :</p> <pre><code>static void *myContext = &amp;myContext; -(void)registerForToolbarItems:(UIViewController*)viewController { [viewController addObserver:self forKeyPath:@"toolbarItems" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:myContext]; } -(void)unregisterForToolbarItems:(UIViewController*)viewController { [viewController removeObserver:self forKeyPath:@"toolbarItems" context:myContext]; } </code></pre> <p>The observe action will handle receiving the new values and forwarding them to the mirrorVC:</p> <pre><code>- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if(context == myContext) { id newKey = [change objectForKey:NSKeyValueChangeNewKey]; id oldKey = [change objectForKey:NSKeyValueChangeOldKey]; //no need to mirror if the value is the same if ([newKey isEqual:oldKey]) return; //nil values comes packaged in NSNull if (newKey == [NSNull null]) newKey = nil; //handle each of the possibly registered mirrored properties... if ([keyPath isEqualToString:@"navigationItem.leftBarButtonItem"]) { self.mirrorVC.navigationItem.leftBarButtonItem = newKey; } //... //as many more properties as you need forwarded... else if ([keyPath isEqualToString:@"toolbarItems"]) { [self.mirrorVC setToolbarItems:newKey animated:YES]; } } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } </code></pre> <p>Then in your container, at the right moments, you register and unregister</p> <pre><code>[_selectedViewController unregister...] _selectedViewController = selectedViewController; [_selectedViewController register...] </code></pre> <p>You must be aware of a potential pitfall though: not all desirable properties are KVO compliant, and the ones that do aren't documented to be - so they can stop being or misbehave at any time. The toolbarItems property, for example, is not. I created a UIViewController category based on this gist ( <a href="https://gist.github.com/brentdax/5938102" rel="nofollow">https://gist.github.com/brentdax/5938102</a> ) that enables KVO notifications for it so it works in this scenario. Note: the gist above wasn't necessary for UINavigationItem, iOS 5~7 sends out proper KVO notifications for it, with that category I would get double notifications for UINavigationItems. It worked flawlessly for toolbarItems!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload