Question

I would like to know which is the best way to send parameters between viewControllers. I know there are two possibilities, pass the parameters in the public properties after the init call.

ViewController *vc = [ViewController alloc] init];
vc.propertyOne = @"whatever";
vc.propertyTwo = @"whatever2";

Or creating a new Custom init like

initWithProperty:(NSString *)prperty1 andPropertyTwo:(NSString *)property2 
{
  self = [super init];
    if (self) {
        self.propertyOne = prperty1;
        self.propertyTwo = property2;
    }
    return self;

}

ViewController *vc = [[ViewController alloc] initWithProperty:@"whatever andPropertyTwo:@"xxxx"];

I would like to know Advantages and disadvantages of each one, and "when" and "why" is better use one of them.

Was it helpful?

Solution

You should pass parameters to a custom init method if those values are needed immediately in the implementation of the init method.

You should use properties, that are set just after calling alloc/init, if you have several properties to set and those properties aren't needed in the init method itself.

Many times the properties aren't needed until viewDidLoad (in the case of a view controller) so using properties is cleaner. You don't want to end up with an init method with a dozen parameters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top