views:

232

answers:

3

I have an area of a Window (in my MainMenu.xib) which I'd like to populate dynamically with unrelated "views" such as an NSTable, IKImageBrowserView etc. at different points of time depending on some user-selected criteria.

  • How do I define this area of the window such that it can be "replaced" with different views?
  • How do I attach a table or some other view to this area of the window?

(Is it enough to place a generic NSView there and add a subview each time? I'm fairly new to Cocoa, so any pointers are welcome)

+1  A: 

There are a couple of ways to do it. I find it's easy to put in a "custom nsview", and replace it with the real view in windowDidLoad. It'd look something like this:

@interface MyController : NSWindowController
{
    IBOutlet NSView* dummyView;
}
@end

@implementation MyController
-(void)windowDidLoad{
    NSView* actualView = ...; //create the real view here
    [actualView setFrame:[dummyView frame]];
    [actualView setAutoresizingMask:[dummyView autoresizingMask]];

    NSView* superview = [dummyView superview];
    [dummyView removeFromSuperview];
    [superview addSubview:actualView];

    dummyView = actualView; //just incase dummyView is ever used again
}
@end
Tom Dalling
A: 

Personally I would add 2 views for the 2 section. Say a topView and a bottomView link them to your code using the linking in interface builder. From there you can manage the content of each view with:

addSubview (is a function of the object that will receive the uiview or object)

you can use this to add individual objects or views to your two main views like:

[topView addSubview:roundedUIButton_instance];

OR

[topView addSubview:another_view_instance];

removeFromSuperview (is a funtion of an objects used to remove its self)

[roundedUIButton_instance removeFromSuperview];

OR

[another_view_instance removeFromSuperview];
abe
+6  A: 

In modern Cocoa, this is the job of view controllers. Cathy Shive has some good blog posts about them (1, 2, 3), and co-authored a more useful view controller class than the basic NSViewController.

Peter Hosey