user278859
user278859

Reputation: 10519

Is it ok to initialize a class with your own initWithFrame?

I am converting an iPhone app to a Universal app. I have a NIB view which I want to use on the iPad as is but resized and positioned. On the iPhone I am initializing normally with initWithNibName...

 EventEditViewController *eventEditViewController = [[EventEditViewController alloc] initWithNibName:@"EventEditViewController" bundle:nil];

I found that this did not work well for me on the iPad for various reasons. So I created my own initialization method to call instead when running on the iPad...

EventEditViewController *eventEditViewControllerForIPad = [[EventEditViewController alloc] initWithFrame:iPadFrame eventDate:longDate event:eventName delegate:self];

This solved a couple of problems. One how to resize and position the view where I wanted it and how to properly initialize certain variables. I am actually passing more variables than you see here.

It works really well, but I just now noticed that, unlike initWithNibName viewDidLoad fires before my initWithFrame method. I only found this out because a variable I was trying to access in viewDidLoad was showing up as a zombie and I thought I was initializing it in my initWithFrame method.

I was surprised by this behavior. Is it normal? It doesn't make sense to me that the view would be loaded before the named initMethod in the alloc/init call.

I am now wondering if what I am doing might not be a good thing. Like I said it works really well, but should I not use my own initialization method here?

If it's ok to do it this way, maybe someone can explain why the view loads before the init method.

Thanks,

John

Upvotes: 3

Views: 331

Answers (3)

indiantroy
indiantroy

Reputation: 1503

From Apple's PageControl source code

// load the view nib and initialize the pageNumber ivar
- (id)initWithPageNumber:(int)page
{
    if (self = [super initWithNibName:@"MyView" bundle:nil])
    {
        pageNumber = page;
    }
    return self;
}

You can have your own custom init method defined in that EventEditViewController and you can use a custom method like above to initialize your viewController and set as many member variables as you want like iPadFrame, longDate, eventName etc in your case.

Just make sure you call it exactly as above as it's important to call super implementation in such custom init methods.

Also just to shed more light on where you should release arrays you created in viewDidLoad method, it's the dealloc method first in addition to viewDidUnload. The reason behind this is viewDidUnload method doesn't always get called. It gets called only when application starts receiving memory warnings. As compared to this, the method dealloc gets automatically called always when you release the initialized viewController and it's retain count reaches 0. You should release the arrays you initialized viewDidLoad method and your other retain properties in dealloc method.

Also keep in mind that when the app receives memory warning, it's actually a chance to free up additional memory. Also the viewDidUnload method gets called for all the viewControllers in memory except visible one at that time.

Upvotes: 1

Caleb
Caleb

Reputation: 124997

If your -initWithFrame:... method is accessing the view controller's view property, -viewDidLoad will be called before the init method completes because the view accessor will cause the view to be loaded.

As for whether it's okay to use your own method, it should be fine provided that your init method calls the designated initializer for the class.

Upvotes: 2

Mugunth
Mugunth

Reputation: 14499

Initializing member variables should be done in viewDidLoad or awakeFromNib. awakeFromNib is the first method that gets called when a view comes to life from a Xib.

It's preferred to use viewDidLoad for allocating memory for huge arrays since you can deallocate them in viewDidUnload.

Both navigation controller and tab bar controller uses view loading methods to unload views when other views demand more memory.

Allocating in anyother methods should be avoided as far as possible.

Upvotes: 1

Related Questions