如何删除子视图(或视图的所有子视图) [英] How to remove a subview (or all subviews of a view)

查看:97
本文介绍了如何删除子视图(或视图的所有子视图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,其中我alloc和init一个

  UIView(`tabsClippedView = [[[UIView alloc] initWithFrame :tabsClippedFrame] autorelease];`)。 

此视图添加了另一个视图

 (`tabsView = [[[UIView alloc] initWithFrame:tabsFrame] autorelease];`)。 

然后我启动几个按钮

 (例如`UIButton * btn = [[[UIButton alloc] initWithFrame:frame] autorelease];`)

并将它们添加到视图的子视图。



现在有时,我需要删除所有的按钮和再次分配。是删除整个视图还是删除我添加了按钮的子视图的最佳方法?



我需要做什么(没有内存泄漏等)? 很简单

  self.tabsView = nil; 

足以删除视图及其所有子视图strong>



或者最好也删除superview,从头开始:

  self.tabsClippedView = nil; 


解决方案

由于您的UIView是自动释放的,它从superview。对于它有 removeFromSuperview 方法。



所以,你只想调用 [self.tabsView removeFromSuperview] 。只要您的属性声明设置为 retain ,那就是您需要的。


I have a method in which I alloc and init an

UIView (`tabsClippedView = [[[UIView alloc] initWithFrame:tabsClippedFrame] autorelease];`).

This view has another view added to it

(`tabsView = [[[UIView alloc] initWithFrame:tabsFrame] autorelease];`).     

Then I initiate a couple of buttons

(e.g. `UIButton* btn = [[[UIButton alloc] initWithFrame:frame] autorelease];`)

and add them to the subview of the view.

Now from time to time, I need to delete all the buttons and assign them again. Is the best way to delete the entire view or simply the subview to which I added the buttons?

How would I need to do this (without memory leaking etc.)? Would a simple

self.tabsView = nil;

suffice to delete the view and all of its subviews (i.e. the buttons)?

Or would it better to delete the superview as well, to start entirely from scratch:

self.tabsClippedView = nil;

解决方案

Since your UIView is autoreleased, you just need to remove it from the superview. For which there is the removeFromSuperview method.

So, you'd just want to call [self.tabsView removeFromSuperview]. As long as your property declaration is set to retain that's all you'll need.

这篇关于如何删除子视图(或视图的所有子视图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆