开发者

initWithNibName: what kind of custom initialization?

开发者 https://www.devze.com 2023-04-03 17:45 出处:网络
edit : ok, what i\'ve learned is you might choose between initWithNibName or initWithCoder, depending if you use a .xib or not. And \"init\" is just not the constructor method for UIVIewController.

edit : ok, what i've learned is you might choose between initWithNibName or initWithCoder, depending if you use a .xib or not. And "init" is just not the constructor method for UIVIewController.

This might seem to be a fairly simple question, but I'm not sure about the answer : I've read that this method "is only used for programatically creating view controllers", and in the doc : "It is loaded the first time the view controller’s view is accessed"

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

Ok so to understand it a bit more :

What would开发者_开发知识库 you write as "custom initialization" into this method?

When should you implement this method, like that in the code, if you can just write it after allocating your viewController (example : MyVC *myvc = [[MyVC alloc] initWithNibName:...bundle...];)

Thanks for your answer


Usually I do this:

// Initialize controller in the code with simple init
MyVC *myVC = [[MyVC alloc] init];

Then do this in my init method:

- (id)init {
    self = [super initWithNibName:@"MyVC" bundle:nil];

    if (!self) return nil;

    // here I initialize instance variables like strings, arrays or dictionaries.

    return self;
}

If controller needs some parameters from the initializee, then I write custom initWithFoo:(Foo *)foo method:

- (id)initWithFoo:(Foo *)foo {
    self = [super initWithNibName:@"MyVC" bundle:nil];

    if (!self) return nil;

    _foo = [foo retain];

    return self;
}

This allows simplification of initialization as well as extra initializers for your view controller if it can be initialized from different locations with different parameters. Then in initWithFoo: and initWithBar you'd simply call init which calls super and initializes instance variables with default values.


It's an init method, so you initialize everything you need to be initialized when you begin your work in your view controller. Every object ivar will be automatically initialized to nil, but you can initialize a NSMutableArray to work with or an BOOL that you want to be a certain value.

You implement this method every time you have something to initialize, as stated previously. You generally don't initialized things after allocating your view controller, that way you don't need it to do it every time you use your view controller (as you may use it at different place in your application). It's also the best practice.


I usually put in there configuration stuff that I know it wont change.

For example, the ViewController's title:

self.title = @"MyTitle";

Or if this is one of the main ViewController in a TabBar application. That is, it owns one of the tabs, then I configure its TabBarItemlike this:

UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"something" 
                     image:[UIImage imageNamed:@"something.png"] 
                       tag:0];

self.tabBarItem = item;

There is all sorts of things you can do there.

0

精彩评论

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