How to load an NSView on an NSWindow dynamically?
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.
精彩评论