Currently I’m working on yet another Mac app, where I have a NSTabView with multiple tabs that each represent a view controller.
I noticed that when I set my view controller’s title with
self.title = @"some other title"
nothing changes. So I wrote a small NSTabViewItem subclass that updates the title automatically by observing the title key of the view controller.
The code is on github and you can find the important part below.
@implementation GNTabViewItem -(void)dealloc { [self.viewController removeObserver:self forKeyPath:@"title"]; } -(void)setViewController:(NSViewController *)viewController { [self.viewController removeObserver:self forKeyPath:@"title"]; [super setViewController:viewController]; [self.viewController addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil]; } -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if([keyPath isEqualToString:@"title"] && object == self.viewController) { self.label = self.viewController.title; return; } [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } @end
Recent Comments