开发者

Set args in -(id)init

开发者 https://www.devze.com 2023-04-04 13:48 出处:网络
is possible pass an args on an init method? I\'m working with UITableViewController classes and UINavigatorController, when I push a new view whit:

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 controlle开发者_如何学Gor, is it possible?


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.


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消