Tabbed windows, take 2

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
2
3
4
views = @{
        @"General": generalPreferencesView,
        @"Catalogue": cataloguePreferencesView
        };

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

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