views:

88

answers:

2

If you want to modify the default behaviour of a View Controller by changing the value of one of its properties, is it considered better form to

  1. instantiate the class and set its property directly, or

  2. subclass it and override the property?

With the former it would become the parent View Controller's responsibility to configure its children, whereas with the latter the children would effectively configure themselves.

EDIT: Some more information:

The class I am referring to is FetchedTableViewController, a subclass of UITableViewController that I made to display the results of a Core Data fetch operation.

There are two places I want to display the results of a fetch, and they each have different fetch requests.

I'm trying to decide whether it's better to create a subclass for each one, and override the fetchRequest property, or make it the responsibility of the parent controller to set the fetchRequest property for its children.

A: 

Generally speaking, Cocoa is designed to minimize subclassing (hence the wide use of delegates). That said, UIViewController is a class designed specifically TO be subclassed. Since it's better to keep code 'local', I'd recommend subclassing.

Ben Gottlieb
I just realised there is some ambiguity in my question. The class I'm discussing is actually a subclass of UITableViewController already. Let me update my question to give some more information.
robinjam
A: 

From your description, this sounds like a place where you should be providing your own init:

- (id)initWithParamterINeed: (BOOL)aParameter;
{
    if (( self = [super initWithNibName: @"MyNibName" bundle: nil] )) {
        parameter = aParameter;
    }
    return self;
}
Steven Fisher
I would love to be able to do that, but unfortunately the controllers are instantiated in IB and so I'm constrained by its inability to use custom init methods.
robinjam
Huh. I had no idea you could do that. Sounds like I have something to investigate later, because it sounds like something I should read up on. Thanks for the comment. :)
Steven Fisher
Whenever you drag an object from the library into your interface in IB, you are effectively instantiating it. The only exceptions are proxy objects like File's Owner and First Responder. Unfortunately IB isn't clever enough to use custom init methods, so I have to use properties instead ;)
robinjam
I'm aware of how to instantiate object, I'm just not sure why you'd instantiate a UIViewController subclass from a xib and how you'd handle it from the code.
Steven Fisher
I see what you mean. Here's how I've set up my interface - my application's UI is a drill-down navigation interface. My FetchedTableViewController is connected to an outlet on its "parent" view controller, i.e. the one that is displayed before it. Its parent pushes it onto the navigation stack when it needs to be displayed.
robinjam