objective-c - 如何删除 Objective-C 中的 subview ?

标签 objective-c subview

我已通过编程方式将 UIButton 和 UITextView 作为 subview 添加到我的 View 中。

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];

我需要在单击按钮时删除所有 subview 。

我试过:

[self.view removeFromSuperView]

但它不起作用。

最佳答案

移除你添加到 View 的所有 subview

使用下面的代码

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

关于objective-c - 如何删除 Objective-C 中的 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3005540/

相关文章:

ios - 如何自动处理 iOS 中的相对定位?

ios - 如何检查 subview 是否为按钮

iphone - Obj-C,货币值(value)与可用数量,发行

objective-c - 更改 UITableViewCell 的选定状态

Iphone 和从右到左的文本

ios - ScrollView 上不需要的 subview 反弹

iphone - 更改 iPhone 上 subview 的 z 顺序

objective-c - 如何检查 NSWindow 是否可见

objective-c - 调用新窗口时是否可以显示停靠图标?

android获取 View 的所有 subview