Search code examples
objective-ccocoaxcode4iboutletnsviewcontroller

Multiple NSViews into one Xib file


For a couple of days, I have been trying to figure out how to work with multiple NSViews into a single .xib file. I have added a NSViewController subclass which I am using as my main controller, it has two NSViews with outlets for two custom views. I subclassed it into two NSViewController (PumpController & TransactionController) to control the content of both custom views.

I can get half a result when I bind the view of the MainController (File's owner) to one of the custom views. Then, when i call [self.view addSubview:SomeStuff] into one of the NSViewController, I get the right result but only in the view I connected the file's owner with. I need to get result in both views but I can't have more than one file's owner with view outlet. Here's a picture

Multiple views in a NIB. Link: https://i.sstatic.net/gwSAq.jpg

Also, even if I bind my outlets mentioned above with the right view, I can't get "outlet".bounds.size.width result from PumpController. self.view.bounds.size.width gives me the result of the File's owner view, not the result of PumpController view.

I need some help to explain me how to setup each of my custom views to each NSViewController in order to get this works.

Sorry for my english :s

Thanks in advance


Solution

  • I do it as follows:

    • In your main XIB place NSBox instances (with no border and heading) wherever you want your ViewController's views to be displayed.
    • Connect the boxes to File's Owner by IBOutlets.
    • In the File's Owner of that XIB (likely = AppDelegate) instantiate your NSViewControllers and set the contentView properties of the NSBox instances to the view properties of the NSViewControllers. Like so: [self.leftBox setContentView:[yourViewController view]].