开发者

iPhone view loaded many times... but never unloaded?

开发者 https://www.devze.com 2023-01-27 06:10 出处:网络
My iPhone app is based on a common \"utility template\" like Apple\'s own Weather app. I click on the info-button and it flips the screen around.I click on my done button... and it flips back.That pa

My iPhone app is based on a common "utility template" like Apple's own Weather app.

I click on the info-button and it flips the screen around. I click on my done button... and it flips back. That part all seems to work ok.

I've placed NSLog() statements into each of 4 methods in my FlipSideViewController.m

viewDidLoad
viewWillAppear
viewDidUnload
viewWillDisappear

Shouldn't I see viewDidLoad and viewDidAppear being called when I flip TO my FlipSide. And then see viewWillDisappear and viewDidUnload when I flip back?

Instead, I never see any viewDidUnload call. But I DO see another viewDidLoad each time I flip TO my FlipSide. It's that wrong?

Flipping back and forth, again and again, I would see:

viewDidLoad
viewWillAppear
viewWillDisappear

viewDidLoad
viewWillAppear
viewWillDisappear

viewDidLoad
viewWillAppear
viewWillDisappear

Doesn't that mean the vi开发者_如何学Goew reloaded 3 times... but unloaded 0 times? Shouldn't there be "matching" load/unload and appear/disappear methods happening here?


I thought so too at first, but apparently that's not the case.

The viewDidUnload method actually only gets called when the view controller gets a memory warning.

Nonetheless, the view will be released when the view controller is dealloc'd.

So, if you are releasing stuff like IB outlets in viewDidUnload, that's good, but you have to release them in dealloc as well, because viewDidUnload won't be called under normal circumstances (i.e., if you don't get memory warnings).

EDIT: to release the view you just have to call dealloc on the super class UIViewController in your dealloc:

- (void) dealloc
{
    // release your stuff, anything that you alloc or retain in your class

    // then call `dealloc` on the super class:
    [super dealloc];
}

in the viewDidUnload method you only have to release things that should get unloaded along with your view, usually things that you connected to IBOutlets in interface builder.

- (void) viewDidUnload
{
    // if the property was declared with the "retain" keyword, you can
    // release it simply by setting it to nil like this:
    self.myOutlet = nil;
}
0

精彩评论

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

关注公众号