| Sign In/My Account | View Cart |
| Article: |
The Cocoa Controller Layer | |
| Subject: | Override add? | |
| Date: | 2004-04-24 09:47:10 | |
| From: | kristianmonsen | |
| Great tutorial on the cocoa bindings. Is there any easy way of overriding the add method? I would like new books to have a default title and author when added to the table so it's easier to see that there is an item there. | ||
Showing messages 1 through 3 of 3.
- (id)init
{
self = [super init];
if (self != nil) {
[self setTitle:@"Unknown Title"];
[self setAuthor:@"Unknown Author"];
}
return self;
}
First, subclass NSArrayController in Interface Builder. Nmae it something like MyController. Also, create the files for the new class in your project.
Next, select the BookController instance that was created before and change its 'custom class' to MyController.
Go back to Xcode. Add the following line to MyController.h (it doesn't really matter, but I got a compiler warning otherwise):
#include "Book.h"
Now, override the -(id)newObject: method in MyController.m as follows:
- (id)newObject
{
id newBook = [super newObject];
[newBook setTitle: @"Unknown Title"];
[newBook setAuthor: @"Unknown Author"];
return newBook;
}
Just drag MyController.h into IB, save everything and compile!