Cocoa - Display xib on another xib

前端 未结 2 1317
無奈伤痛
無奈伤痛 2021-02-10 05:55

Can anyone tell me how (or direct me to info on) displaying a .xib (nib) on another .xib (nib).

How ever I wish to place it so I can programically move it around the mai

2条回答
  •  旧巷少年郎
    2021-02-10 06:33

    You can easily load a view from another nib using NSViewController. In your nib you should just set File's Owner's custom class to NSViewController and hook up the view outlet of File's Owner to point to the view you want to load. You can then just do this:

    //create an NSViewController and use it to load the nib
    NSViewController* vc = [[NSViewController alloc] initWithNibName:@"YourNibName" bundle:nil];
    //get the view from the view controller
    NSView* loadedView = [vc view];
    //release the view controller so we don't leak
    [vc release];
    //add the view as a subview of your main view
    [mainView addSubview:loadedView];
    //position the view
    [loadedView setFrameOrigin:NSMakePoint(100.0, 100.0)];
    

    You don't need to do anything in drawRect:. The subview will draw itself, and drawRect: will be called automatically if you move the subview.

    You should read the View Programming Guide for Cocoa. It is critical to understanding how views work, and it is clear from your question that you do not yet have that understanding.

    You should also read the Cocoa Drawing Guide.

提交回复
热议问题