我已通过编程方式将 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/