开发者

Should I release subviews of UIView in viewDidUnload?

开发者 https://www.devze.com 2023-02-12 11:38 出处:网络
Should I release my subviews of UIView in the viewDidUnload when I have references to them as instance variables which retains them? I have build the GUI programmatically. I should do that right? Sinc

Should I release my subviews of UIView in the viewDidUnload when I have references to them as instance variables which retains them? I have build the GUI programmatically. I should do that right? Since both uiview and ivars retain then the objects would have 2 in retain-count, when view receives e.g. memory-warning then the UIView will release the subviews, but they still have +1 in retain count so I have to setself.myIvar = nil; In the viewDidU开发者_如何学Gonload?

Thanks for your time.


You actually can release all retained subviews in viewDidUnload. But I used to do it in another way:

-(void) viewDidLoad {
    someInstanceView1 = [[UIView alloc] init];
    [self.view addSubview: someInstanceView1];
    [someInstanceView1 release];

    someInstanceView2 = [[UIView alloc] init];
    [self.view addSubview: someInstanceView2];
    [someInstanceView2 release];

    //etc...
    //you have a references to someInstanceView1 and someInstanceView2 with retained counts 1
}

In this case even if memory warning will arise, the view controller will remove all it's view subviews. And then call viewDidLoad again. So there would be no leaks and you don't need to care about releasing that ivars at all cause the only owner (it has the strong reference to the views) is the view controller's view and it will release them automatically.

0

精彩评论

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