2
votes

I have a main .xib view with parts of it being made up of custom views. At the same time I have also created separate .xib subviews (together with their respective .h and .m files). These custom classes were then connected to the custom views in the main .xib

This setup works fine however I would like to have a number of NSViewController(s) control each of the different custom views. What is the recommended way to do this?

Eg. Main.xib > contains 'custom views' > each using an NSView custom class and designed in it's own .xib

An NSViewController class would respond to events occurring in one of these custom views instead of the NSViewController tied to Main.xib

2
I think you can do this by sub-classing NSViewController and placing the required number of those new view controllers in the main xib file (just like the app delegate). Then you can graphically hook them up with the associated custom view in the window and have them load their own associated xib on awakeFromNib. You would hold on to these child controllers through outlets on your main view controllerpco494

2 Answers

2
votes

I've ended up creating NSViewControllers with XIB files instead of the custom views I had previously.

I then created NSBox components for every custom view that I had. I connected each of these to the main NSViewController via IBOutlet(s).

Finally, I attached each custom view to the dedicated NSBoxes via the IBOutlets as follows:

- (void) awakeFromNib{
   [super awakeFromNib];


    //instantiate custom view controller
   CustomViewController* customViewController = [[CustomViewController alloc] initWithNibName:@"CustomViewController"
                                                  bundle:nil];
   [self.customNSBoxView setContentView:[customViewController view]];
}
0
votes

What I've tended to do is just create a new NSViewController subclass and choose the 'Create XIB' option.

From there, I instantiate the view controller subclass and add it to the view hierarchy in code.

This doesn't completely do what you are suggesting, but it does keep things more modular / easier to test. The downside is that its challenging then to setup constraints between the parent and children. I've tended to do this manually as well, or add the subcontroller's view into a NSStackView which gives you some constraints.

There's probably a better way, but this is what we used in our multi-xib project.