Tabbed windows, take 2

Published
25 September 2013
Tagged

After my previous post on how to do tabbed windows in Cocoa, a friend pointed out that I'm going about this all wrong, and using NSTabView is inefficient and introduces unnecessary overhead.

Turns out you can do this much more simply by just adding another NSView alongisde your current window:

An orphaned NSView, waiting for a friend

An orphaned NSView, waiting for a friend

You can hook this up to an IBOutlet in your controller, so you can access it during runtime. Then all you need to do is change the window's contentView when you want to switch views. I've done it by hooking these views up to a NSDictionary during controller initialisation:

views = @{
        @"General": generalPreferencesView,
        @"Catalogue": cataloguePreferencesView
        };

I've then got the following IBAction set up as every NSToolbarItem's selector:

-(IBAction)toolbarButtonClicked:(id)sender {
  window.contentView = views[[sender label]];
}