如何删除 Objective-C 中的子视图?
我已以编程方式将 UIButton 和 UITextView 作为子视图添加到我的视图中。
notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
notesDescriptionView.backgroundColor = [UIColor redColor];
[self.view addSubview:notesDescriptionView];
textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)];
[self.view addSubview:textView];
printf("\n description button \n");
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button
addTarget:self action:@selector(cancel:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"OK" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0);
[self.view addSubview:button];
单击按钮时我需要删除所有子视图。
我已经尝试过:
[self.view removeFromSuperView]
但它不起作用。
I have added UIButton and UITextView as subviews to my view programmatically.
notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
notesDescriptionView.backgroundColor = [UIColor redColor];
[self.view addSubview:notesDescriptionView];
textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)];
[self.view addSubview:textView];
printf("\n description button \n");
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button
addTarget:self action:@selector(cancel:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"OK" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0);
[self.view addSubview:button];
I need to remove all subviews when the button is clicked.
I have tried:
[self.view removeFromSuperView]
but it's not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要删除添加到视图中的所有子视图,
请使用以下代码
to remove all the subviews you added to the view
use the following code
我假设您正在从与上述代码片段相同的类中的方法调用
[self.view removeFromSuperView]
。在这种情况下,
[self.view removeFromSuperView]
会从其自己的超级视图中删除 self.view,但 self 是您希望从中删除子视图的对象。如果您想删除对象的所有子视图,则需要这样做:也许您希望将这些子视图存储在
NSArray
中,并调用removeFromSuperview
循环遍历该数组code> 该数组中的每个元素。I assume you're calling
[self.view removeFromSuperView]
from a method in the same class as the above snippet.In that case
[self.view removeFromSuperView]
removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:Perhaps you'd want to store those subviews in an
NSArray
and loop over that array invokingremoveFromSuperview
on each element in that array.我一直很惊讶 Objective-C API 没有一个简单的方法来从 UIView 中删除所有子视图。 (Flash API 确实如此,而且您最终会非常需要它。)
无论如何,这是我用于此目的的小辅助方法:
编辑:刚刚在这里找到了一个更优雅的解决方案: 从 self.view 中删除所有子视图的最佳方法是什么?
现在使用它如下:
我更喜欢它。
I've always been surprised that the Objective-C API doesn't have a simple method for removing all sub views from a UIView. (The Flash API does, and you end up needing it quite a bit.)
Anyway, this is the little helper method that I use for that:
EDIT: just found a more elegant solution here: What is the best way to remove all subviews from you self.view?
Am using that now as follows:
I like that better.