Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [120]

By Root 840 0
We are subclassing NSViewController so that each of our view controllers will have an NSManagedObjectContext. Use the project navigator to delete the ManagingViewController.xib file that Xcode created for us alongside the .h and .m; we will create XIBs for our ManagingViewController subclasses. Edit ManagingViewController.h:

#import

@interface ManagingViewController : NSViewController {

NSManagedObjectContext *managedObjectContext;

}

@property (strong) NSManagedObjectContext *managedObjectContext;

@end

In ManagingViewController.m, remove the initializer created by the Xcode template, synthesize managedObjectContext:

#import "ManagingViewController.h"

@implementation ManagingViewController

@synthesize managedObjectContext;

@end

Create ViewControllers and their XIB files


Now you are going to create two separate views that will be swapped into the box you created. Each view has its own controller: a subclass of ManagingViewController. Thus, you are going to do the same basic steps twice:

1. Create a subclass of ManagingViewController to act as File’s Owner.

2. Create a XIB file for the view.

One view will be for looking at departments in a company. The other view will be for looking at the employees of a company. You will do the Departments view first.

In Xcode, create an Objective-C class that subclasses ManagingViewController, and name it DepartmentViewController. In DepartmentViewController.h, import ManagingViewController.h.

In Xcode, create a new view XIB (Mac OS X -> User Interface -> View). Name it DepartmentView. Open DepartmentView.xib. Put a few text fields and a couple of butons on the view. (We aren’t going to use these controls; we just want you to see something interesting when the view appears in the box.)

In the Identity Inspector, set the class of File’s Owner to be DepartmentViewController. Control-click on File’s Owner and drag to the view (labeled Custom View) to set the view outlet. (The view outlet is defined in NSViewController.) Save the XIB file.

Figure 31.4. Introducing the View Controller to Its View

Back in DepartmentViewController.m, give the controller a XIB and a title in its init method:

- (id)init

{

self = [super initWithNibName:@"DepartmentView"

bundle:nil];

if (self) {

[self setTitle:@"Departments"];

}

return self;

}

OK? Good. Do it again for EmployeeViewController:

• Create a class called EmployeeViewController, and make it a subclass of ManagingViewController.

• Make a XIB file (named EmployeeView.xib) with a view.

• Put a text view (or something else pretty) on the view.

• Set the class of File’s Owner to be EmployeeViewController.

• Set the view outlet of File’s Owner to point to the view.

• Add an init method to EmployeeViewController.m:

- (id)init

{

self = [super initWithNibName:@"EmployeeView"

bundle:nil];

if (self) {

[self setTitle:@"Employees"];

}

return self;

}

Add View Swapping to MyDocument


Now you need to create instances of the controllers in MyDocument and add them to the viewControllers array. Add this to MyDocument.m:

#import "MyDocument.h"

#import "DepartmentViewController.h"

#import "EmployeeViewController.h"

@implementation MyDocument

- (id)init

{

self = [super init];

if (self) {

viewControllers = [[NSMutableArray alloc] init];

ManagingViewController *vc;

vc = [[DepartmentViewController alloc] init];

[vc setManagedObjectContext:[self managedObjectContext]];

[viewControllers addObject:vc];

vc = [[EmployeeViewController alloc] init];

[vc setManagedObjectContext:[self managedObjectContext]];

[viewControllers addObject:vc];

}

return self;

}

Create the method that swaps the view in:

- (void)displayViewController:(ManagingViewController *)vc

{

// Try to end editing

NSWindow *w = [box window];

BOOL ended = [w makeFirstResponder:w];

if (!ended) {

NSBeep();

return;

}

// Put the view in the box

NSView *v = [vc view];

[box setContentView:v];

}

Declare that method in MyDocument.h (also

Return Main Page Previous Page Next Page

®Online Book Reader