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:

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]];


Leave a comment

Sorry, you need to enable javascript for commenting. That’s one of the drawbacks of having a static site blog I guess.