1
votes

I am writing a Cocoa Framework (not an Application), it contains the definition of a customized NSView; the framework will be loaded in other applications, and the customized NSView will be dragged to the GUI of the applications and become initialized

The question is that I want to include a XIB file in the Framework

I want to add a button and a label to the XIB (in the framework), but the view in the application that consumes the framework, won't show the button and the label

I already set the File's Owner of XIB to the custom NSView in the framework

What else should I do?

2

2 Answers

1
votes

Have the view load the nib and move the button, label, etc., from the view in the nib to itself.

It'll be easiest to do this just by getting the subviews of the nib's view and doing this for all of them.

If your nib uses Auto Layout, I think you'll also need to bring across any constraints owned by the nib's view, and you may also need to edit or replace any constraints that refer to that view (e.g., if a view is set to be X points away from an edge of the nib's view).

You may also need to do extra work to change the new view's frame, or to change the frames of the subviews (whether by relocation, resizing, or both) to match the frame given for the new view.

0
votes

Yeah, I just solved the issue, and I hope the details are helpful to others.

In the framework, there is a complicated subclass of NSView, which contains many controls such as NSSplitView NSOutlineView and IKImageBrowserView, NSPathControl and etc; the framework contains a H file, a M file and a XIB file; H and M define the MyView class; in the XIB, there is a View object whose class is MyView.

On the application side, users need to drag a NSView item onto the main window of their app, and assign an outlet to the view, let's say mainView, and in the applicationDidFinishLaunching function of the "consumer" app, the follow code is necessary

NSBundle *frameworkBundle = [NSBundle bundleForClass:[MyView class]];

NSNib *nibby = [[[NSNib alloc] initWithNibNamed:@"MyView" bundle:frameworkBundle] autorelease];

NSArray *topLevelObjects = nil;

BOOL flag = [nibby instantiateNibWithOwner:nil topLevelObjects:&topLevelObjects];

assert(flag);

for (id topLevelObject in topLevelObjects) {
    if ([topLevelObject isKindOfClass:[MyView class]]) {

        [mainView addSubview: topLevelObject];
        MyView* xView = topLevelObject;
        [xView setFrameSize:mainView.frame.size];
        break;
    }
}

In the code above, the XIB files is loaded, thus the MyView object is initialized, then we fetch it out of the XIB, resize it and add it to the main view of the window