开发者

iPhone SDK Managing Memory for Multiview app

I have an app that programmatically creates labels and text fields based on the contents of a text file. Whenever the view controller is loaded, it creates text fields and labels that are different each time. My problem is I need to clear out the labels and text fields without releasing the view controller since I need to keep track of the view controller. I tried self.开发者_如何学PythonviewController = nil, but I'm pretty sure this results in a memory leak. Is there a way to delete all the subviews of a view?


What Greg meant to say is this:

for (UIView *subview in self.view.subviews) {
    [subview removeFromSuperview];
}

This may not work as you would expect though, as Objective C doesn't like it when you modify an array while you're iterating through it in a for loop. A safer choice would be this:

while ([self.view.subviews count] > 0) { 
    [[self.view.subviews lastObject] removeFromSuperview]; 
}


If you have a view named view, you should be able to remove all of the subviews from the view using this code:

for (UIView *subview in view) {
    [subview removeFromSuperview];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜