10

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 here enter 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.

Community
  • 1
  • 1
TheNextman
  • 12,428
  • 2
  • 36
  • 75

1 Answers1

0

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
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Thank you. I have created a reusable NSView containing my individual components. My question specifically relates to the issue of loading that xib into out views to be re-used. I have added some clarification on my specific sticking points. – TheNextman Feb 25 '13 at 15:45
  • So you stuck on loading the view on required place/view? – Anoop Vaidya Feb 25 '13 at 15:49
  • Yes; technically I can't figure it out and I'm having trouble finding documentation. I don't even understand the theoretical 'Cocoa way' of doing this, to try and figure out the technical side myself – TheNextman Feb 25 '13 at 15:52
  • Please read the specifics of my question. Is NSViewController really the only way to do this? Even if my custom view is a simple grouping of components that require no 'controller' logic? I feel that I should be able to instantiate a view directly from a .xib into a custom view 'placeholder' defined in Interface Builder... – TheNextman Feb 25 '13 at 16:03
  • If you want to follow MVC you should use nsviewController. As you must have felt many a times we create model, view and controller separate, and we could have merged model within controller etc. – Anoop Vaidya Feb 25 '13 at 16:08