Tabbed windows, take 2

Published
2013-08-25
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

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:

1
views = @{
2
        @"General": generalPreferencesView,
3
        @"Catalogue": cataloguePreferencesView
4
        };

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

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