avf
avf

Reputation: 850

iPhone initializing a UIViewController with additional data

I have a custom UIViewController subclass, which gets pushed on a UINavigationController stack. I want to add some data of my own at the time of initialization/pushing. Should I

a) write a custom init method with my data as argument, like this?

MyCustomViewControllerSubclass.m:

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle myCustomData:(NSData *)data{
    if(self = [super initWithNibName:nibName bundle:nibName]){
    //do stuff with my data
    }
    return self;
}

or b) add a property to my viewcontroller that stores my custom data and then add it after initialization?

Is there some advantage/disadvantage in one of these approaches or is there another way to do this?

Very happy for replies!

Upvotes: 3

Views: 3176

Answers (1)

Stefan Arentz
Stefan Arentz

Reputation: 34935

Absolutely, I do this all the time. Even better, forget about the nib name stuff completely and do:

- (id) initWithMyCustomData: (id) customData
{
    if(self = [super initWithNibName: @"MyNibName" bundle: nil]){
        //do stuff with my data
    }
    return self;
}

Upvotes: 6

Related Questions