Question

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!

Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top