Nicolò Ciraci
Nicolò Ciraci

Reputation: 678

Set args in -(id)init

is possible pass an args on an init method? I'm working with UITableViewController classes and UINavigatorController, when I push a new view whit:

[self.navigationController pushViewController:[[Class alloc] init] animated:YES];

I would also pass a string to that controller, is it possible?

Upvotes: 0

Views: 56

Answers (2)

Tim Dean
Tim Dean

Reputation: 8292

It is not only possible, but it is normal and very common. In fact, it is quite common to have multiple initializers in classes where each initializer has a different combination of args, allowing you to initialize a class in different ways.

As you find yourself creating more than one initializer for a class, you should be sure to follow the best practices for making one initializer the "designated initializer". Here is a link to an article that demonstrates the principle of designated initializers.

Upvotes: 2

omz
omz

Reputation: 53561

Sure, just define your own initializer for your view controller subclass:

- (id)initWithStyle:(UITableViewStyle)style andSomeParameter:(NSString *)param
{
    if (self = [super initWithStyle:style]) {
        myInstanceVariable = [param retain];
    }
    return self;
}

(This is for a subclass of UITableViewController.)

Upvotes: 1

Related Questions