开发者

How to load an NSView on an NSWindow dynamically?

I have two nib files. One has an NSWindow and the other one has NSView. I want the NSView to load on the NSWindow at the runtime. I understand that i will have to use the NSWindowController and the NSViewController classes. But i just cant figure out how.

I am new to cocoa..Please guide me as to how to achieve this.

If possible, can u direct me to some sample app or guide for this?

Thanks in advance..

UPDATE:: See comments in line

@interface ViewAvailableItemsWindowController : NSObject {
    IBOutlet NSWindow * viewAvailableItemsWindow; //Window in question
    IBOutlet NSView * viewAvailableItemsView; //View in question

    ItemSearchVie开发者_C百科wController * instanceItemSearchView; //ViewController object 
}

@end


@implementation ViewAvailableItemsWindowController

-(void)awakeFromNib{
    [viewAvailableItemsWindow makeKeyAndOrderFront:nil];
    instanceItemSearchView = [[ItemSearchViewController alloc]initWithNibName:@"ItemSearchView" bundle:nil] ; //Initiating the viewController with the nib for the view.
    [viewAvailableItemsView addSubview:[instanceItemSearchView view]];  //Adding the subview to the window..

}

-(void)dealloc{
    [instanceItemSearchView release];
    [super dealloc];
}

@end


//EDIT regarding to your question you first set the contentView of your NSWindow to be the itemsView. You can do this once eg. in viewDidLoad or init / awakeFromNib:

viewAvailableItemsWindow.contentView = viewAvailableItemsView;

then to make the view dynamic add new items to the window.contentView:

[viewAvailableItemsWindow.contentView addSubview:[instanceItemSearchView view]];

to remove the old and add new ones call removeFromSuperview on the old and then add the new like above


Assuming the window already has a view that you want to insert the loaded view into, you need to tell the existing view in the window to add the new view as a subview.

The View Programming Guide has more information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜