Creating a tab view programmatically with Cocoa/Objective-C

How do I create a tab view programmatically using Objective-C and Cocoa?

13.10.2009 17:21:22

This adds a tab view to a window:

   NSTabView *tabView = [[[NSTabView alloc]
      initWithFrame:NSMakeRect(10,10,300,300)] autorelease];
   [[window contentView] addSubview:tabView];

This adds a tab to the tab view:

   NSTabViewItem *item = [[[NSTabViewItem alloc]
      initWithIdentifier:@"tab1"] autorelease];
   [item setLabel:@"Tab 1"];
   [tabView addTabViewItem:item];

At this point you'll want to add some controls to the tab. You should definitely do this with interface builder. Create a nib with a view, make the file owner a NSViewController. Then do the following:

   NSViewController *viewController = [[[NSViewController alloc]
      initWithNibName:@"myView" bundle:nil] autorelease];
   [item setView:[viewController view]];
13.10.2009 21:41:07
Thank for your comments I'll try this. As far as your final comment goes I cannot use the interface builder because I am trying to build this tab view as a widget that can be used with a version of lisp that uses an objective c bridge. I am not just trying to build an app that has a tab view which is why I was asking about how to do all this programatically.
Mike2012 13.10.2009 21:51:11
You should still be able to use NSViewController. In fact, you can probably assemble the tab view (and its subviews) in IB and use NSViewController to load that.
Peter Hosey 14.10.2009 04:07:47