Cocoa - Show xib on another xib

Can someone tell me how (or direct me to information) by mapping .xib (nib) to another .xib (nib).

No matter how I want to put it, so that I can program it around a basic type like this (which obviously doesn't work)

- (void)drawRect:(NSRect)dirtyRect { NSRect customView = NSMakeRect(pos1, pos1, 200, 100); [[NSBundle mainBundle] loadNibNamed:@"secondXib" owner:self]; NSRectFill (customView); } 

And I want to do this for Mac OS X (not iPhone). (By the way, using xCode 4 incase, it matters)

+4
source share
2 answers

With NSViewController you can easily load a view from another tip. In your nib, you just need to set the File Owner to its own NSViewController class and hook up the view File Owner output to indicate the view you want to load. You can simply 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 do not need to do anything in drawRect: The preview will draw itself, and drawRect: will be called automatically if you move the preview.

You should read View Cocoa Programming Guide . It is imperative to understand how attitudes work, and it is clear from your question that you do not already have such an understanding.

You should also read the Cocoa Drawing Guide .

+9
source

Thank you very much, Another alternative (basically this is not a programming way to execute it) is to add an NSViewController object to your first xib and set it to use the specified nib name. In the second xib, do not forget to specify the class name in the "custom class" field in the view (and NSViewController on the file owner), otherwise it will not work.

+1
source

Source: https://habr.com/ru/post/1402078/


All Articles