David David - 1 month ago 24
iOS Question

Programmatic creation of NSView in Cocoa

i'm used to programming for iOS, and I've become very accustomed to the

UIViewController
. Now, i'm creating an OSX application and i'm having a few general questions on best practice.

In a
UIViewController
I generally setup my views in the
-(void)viewDidLoad
method - I don't actually create a custom
UIView
for the
UIViewController
unless it's really needed - so the
UIViewController
adds view to its own view, removes them, animates them and so forth - first off, is good practice?

And for my main question - what is the best practice in OSX? I like creating interfaces programatically and simply prefer it that way. If i, say create a new custom window and want to manage its view. What's the best way to do it, and where to i instantiate the user interface best?

Summary: How do i construct custom views programatically and set up a best-practice relationship between views and controllers in OSX? And is it considered good practice to use a view controller to create the views within its view?

Kind regards

Answer

To construct the view in code in an NSViewController, override loadView and be sure to set the view variable. Do not call super's implementation as it will attempt to load a nib from the nibName and nibBundle properties of the NSViewController.

-(void)loadView
{
    self.view = [[NSView alloc] init];
    //Add buttons, fields, tables, whatnot
}

For a NSWindowController, the procedure is very similar. You should call windowDidLoad at the end of your implementation of loadWindow. Also the window controller does not call loadWindow if the window is nil, so you will need to invoke it during init. NSWindowController seems to assume you will create the window in code before creating the controller except when loading from a nib.

- (id)initWithDocument:(FFDocument *)document
                   url:(NSURL *)url
{
    self = [super init];
    if (self)
    {
        [self loadWindow];
    }

    return self;
}    
- (void)loadWindow
{
    self.window = [[NSWindow alloc] init];
    //Content view comes from a view controller
    MyViewController * viewController = [[MyViewController alloc] init];
    [self.window setContentView:viewController.view];
    //Your viewController variable is about to go out of scope at this point. You may want to create a property in the WindowController to store it.
    [self windowDidLoad];
}

Some optional fancification (10.9 and earlier)

Prior to 10.10, NSViewControllers were not in the first responder chain in OSX. The menu will automatically enable/disable menu items for you when an item is present in the responder chain. You may want to create your own subclass of NSView with an NSViewController property to allow it to add the controller to the responder chain.

-(void)setViewController:(NSViewController *)newController
{
    if (viewController)
    {
        NSResponder *controllerNextResponder = [viewController nextResponder];
        [super setNextResponder:controllerNextResponder];
        [viewController setNextResponder:nil];
    }

    viewController = newController;

    if (newController)
    {
        NSResponder *ownNextResponder = [self nextResponder];
        [super setNextResponder: viewController];
        [viewController setNextResponder:ownNextResponder];
    }
}

- (void)setNextResponder:(NSResponder *)newNextResponder
{
    if (viewController)
    {
        [viewController setNextResponder:newNextResponder];
        return;
    }

    [super setNextResponder:newNextResponder];
}

Finally, I use a custom NSViewController that overrides setView to set the viewController property when I use my custom views.

-(void)setView:(NSView *)view
{
    [super setView:view];
    SEL setViewController = @selector(setViewController:);
    if ([view respondsToSelector:setViewController])
    {
        [view performSelector:setViewController withObject:self];
    }
}

- (BOOL)acceptsFirstResponder
{
    return YES;
}