I’m a .NET developer trying his hand at an OSX port of a Windows application. I’m using MonoDevelop and MonoMac (with the XCode Interface Builder) to create my UI.
Coming from Windows, I’m trying to understand the Cocoa equivalent of a “UserControl” since I used these extensively in my WinForm version. I have a group of controls (let’s say a label, textbox and button) that I want to package up and reuse in various places throughout my Cocoa UI.
From what I understand, I need to create a “custom view” by subclassing NSView (let’s call it “MyView”). I can create “MyView” in the Interface Builder alright, but when I use it on my main window it always appears empty/blank.
Here’s how I’m placing the control on my main form in IB: I add a Custom View (NSView) from the object library, then change the “class” property under “Custom Class” to “MyView”. It’s not a problem if the control doesn’t render at design time, but it’s totally empty at runtime too.
I suspect I’m missing something really simple/obvious. Are custom views supposed to be used this way? All the examples I’ve found online seem to discuss custom drawing, which I don’t want (I just want to use a bunch of controls together so I can reuse them). Can anyone out there shed some light on this for me?
Thanks in advance.
If you just want a group of standard controls, then you may want a standalone view NIB/XIB. Then you’d load that NIB each time you want to instantiate that group, get the top-level view from it, and insert that view into some other view hierarchy. You’d use an
NSViewController(or a custom subclass) to own and load that NIB.You can also add a free-standing view to a NIB that already contains, say, a window. The NIB’s owner (often an instance of
NSWindowControlleror a subclass) would have an outlet to refer to that free-standing view hierarchy. Your code could then use that view hierarchy at will, inserting it into the window’s view hierarchy at an appropriate place.You don’t typically use a custom view class just to group standard controls. You only make a view subclass to make a non-standard view, one that draws in a custom manner or provides a different manipulation technique.