10
votes

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).

enter image description hereenter image description here

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:

enter image description here

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:

enter image description here

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.

1
you say it, you need code to load your NSView whenever you need it and place it into the view... nothing you can do out-of-the-box with interface builder - Daij-Djan
@Daij-Djan thank you, but I've added some specifics to my question on my particular sticking points - TheNextman
Is it possible to load the .xib from IB? - tofutim

1 Answers

0
votes

If I am getting from your question - You have an XIB having window that contains a passwordTextField and a checkBox.

And you want this xib window to load everytime you need to show password field.


I would suggest you not to create a window, you can create an NSView with a passwordTextField and a checkBox.

And in all the windows draw an empty view and you can load this xib view onto that, so a single object can be used multiple times.


EDIT:

Here is some sample code how I use to load a labelView on main Window.

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize myLabelViewController;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{

    myLabelViewController=[[LabelView alloc] initWithNibName:@"LabelView" bundle:nil];
    [_window setContentView:[myLabelViewController view]];

}

@end

LabelView.h

#import <Cocoa/Cocoa.h>

@interface LabelView : NSViewController

@end

LabelView.m

#import "LabelView.h"

@implementation LabelView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
    }

    return self;
}

@end