[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9. Another Simple Application

This chapter will describe an application, very much like the previous one, but using a slightly different structure. This application builds on the previous application and uses WinController as the NSOwner of the app instead of making it the delegate of NSApplication.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.1 Adding Menu Items

Select the first palette in the palette window, this should be the MenusPalette. The palette will have a bunch of pre-made menu items on it that you can add. We want to keep this simple, so grab the one called “Item” and drag it over to the menu in main menu nib (the menu on the screen, not the one in the objects view). As you have this object over the menu, the copy/paste mouse cursor should appear (it looks something like one box over another box at a 45 degree angle). Where you drop the menu determines it’s position in the menu. You can always drag it to a new position after you’ve placed it by simply selecting and dragging up or down. Once you’ve placed the menu item, double click on the title and change it to “Open”

You can also change the name in the NSMenuItem attributes inspector. Now you must add openWindow: to MyController and make the connection from the “Open” menu item to NSFirst. In the connections inspector, find the “openWindow:” action. You could simply make the connection directly, but this is an exaple to show you that this connection will work as well. Whichever object has First Responder status will be tested to see if it responds to this method.

The implementation for openWindow: in MyController should simply be:

- (void) openWindow: (id) sender

{

winController = [[WinController alloc] init];

}

Also add the winController attribute and an include to allow WinController to be referenced in the MyController.m file.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.2 Making a Controller-based .gorm file

Create a new .gorm file as described in the previous section using the “New Module” menu item. Under “New Module” select “New Empty”. This should produce a .gorm file with only NSOwner and NSFirst. From the WindowsPalette (which should be the second palette in the palette window) drag a window to the location where you want it to appear on the screen. In the window add a button called “Close”.

Go through the same steps you went through previously to create MyController, except for adding the outlets/actions, but this time with the name WinController. Add an outlet called window and an action called “closeWindow:”.

Now, instead of instantiating the class go back to the objects view and select the NSOwner object. After that select the “Custom Class” inspector. Look for the entry for WinController and select it. You now must connect the “window” outlet to the Window you added previously.

Switch back to the objects view, then Control-Drag not to the window on the screen, but to the window’s representation in the objects view. In the connection inspector select the window outlet and click Ok.

Save the .gorm as using the name Controller.gorm in the project directory.

Generate the Controller.h and Controller.h files as described in the previous section.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.2.1 Add the init method to WinController

Add an implementation of the action “closeWindow:” to WinController and also an init which loads the gorm/nib file and declares itself as the owner. Here’s how:

/* All Rights reserved */

#include <AppKit/AppKit.h>

#include "WinController.h"

@implementation WinController

- (id) init

{

if((self = [super init]) != nil)

{

if([NSBundle loadNibNamed: @"Controller" owner: self] == NO)

{

NSLog(@"Problem loading interface");

return nil;

}

[window makeKeyAndOrderFront: self];

}

return self;

}

- (void) closeWindow: (id) sender {

[window close];

}

- (void) dealloc { [super dealloc]; RELEASE(window); }

@end

The Controller gorm will be loaded and the connections will be made to the current instance, i.e. window will point to the window object instantianted in the .gorm file and all actions declared in the .gorm file which are attached to the object NSOwner will be resolved on self.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.3 Running the App

Type the command ‘open Controller.app’ on the command line in the project directory. Once the application has started it should look very much like the first application. Select the “Open” button from the Menu and you should see the second window pop up, now choose close, this will call the method “closeWindow:” which should cause the window to disappear.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by root on July 23, 2013 using texi2html 1.82.