In several parts of my application, I need a control where a user can enter a password. The password field is secure, however I want the user to have the ability to switch the field to plain text and check for typos, etc.
The password field itself is no problem. I subclassed NSSecureTextField to get the behaviour I want. I make it bindable, so I can toggle the display from another control (say, a Checkbox).
Now, I want to reuse this in multiple places within my application. I can simply use my custom secure text field along side a checkbox everywhere I need this. But I would prefer to group these pieces together into a reusable component.
So I create one using interface builder:
Now, how can I use that in my different windows? I have added a 'Custom View' component to my window, and set it's type appropriately:
But at runtime, I just get an empty space.
1 - Presumably because my view is defined in a .xib - I need to do something in code to load my PasswordView instance from the .xib; but what exactly do I need to do? I'm having trouble working this out.
Specifically:
What is the process / API to load an instance of my view from a .xib? I'm struggling to find the documentation on this.
Once I have loaded an instance of my view, how to insert it in place of my Custom View 'placeholder'? Presumably I need an outlet to my custom view, but then what? Do I just assign my loaded instance to the outlet? Am I supposed to add it as a subview to the outlet?
Where should this logic be happening? Inside init, inside AwakeFromNib?
2 - I know I can use an NSViewController for my custom view (example), and load it that way. But do I really need a view controller just for this? It seems I should just be able to create standalone views and instantiate them easily... But perhaps this is not the Cocoa 'way'? It seems to me that there should be a straightforward way of composing a view from multiple .xibs without needing a controller for every sub view (which may just be a text field, or a button), but I'm just not finding it...
Thank you in advance.