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.
Main Topics Oldest First

Showing messages 1 through 2 of 2.

  • Override add?
    2004-07-05 14:19:11  markstone1 [View]

    I just worked out a pretty easy way to set the title/author of newly created books. Hope it helps:

    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!
  • Override add?
    2004-07-05 14:18:56  markstone1 [View]

    I just worked out a pretty easy way to set the title/author of newly created books. Hope it helps:

    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
    {
    NSLog (@"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!