您可以声明特定视图的标签值,并根据需要删除所有视图:
*for (UIView *subview in [self.view subviews])
{
if(查看 isKindOfClass:[UIView 类])
{
if (subview.tag == 101 || subview.tag == 102)
{
[subview removeFromSuperview];
}
}
}*
此行下方有一个示例代码:在此代码中,由 xib 创建并调用 btnClicked 方法的按钮“btn”
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myViewNew = [[UIView alloc] initWithFrame:CGRectMake(50, 40, 150, 150)];
[myViewNew setTag:102];
[myViewNew setBackgroundColor:[UIColor redColor]];
myView = [[UIView alloc] initWithFrame:CGRectMake(50, 40, 150, 150)];
[myView setTag:101];
[myView setBackgroundColor:[UIColor blueColor]];
btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn1 setFrame:CGRectMake(50, 50, 50, 50)];
btn1.titleLabel.text = @"btn1";
[myView addSubview:btn1];
[btn1 addTarget:self action:@selector(btn11Pressed:) forControlEvents:UIControlEventTouchUpInside];
btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn2 setFrame:CGRectMake(50, 50, 50, 50)];
btn2.titleLabel.text = @"btn2";
[myViewNew addSubview:btn2];
[btn2 addTarget:self action:@selector(btn2Pressed) forControlEvents:UIControlEventTouchUpInside];
}
-(void)btn2Pressed
{
for (UIView *subview in [self.view subviews])
{
if(view isKindOfClass:[UIView class])
{
if (subview.tag == 101 || subview.tag == 102)
{
[subview removeFromSuperview];
}
}
}
}
-(void) btn11Pressed:(id)sender
{
[self.view addSubview:myViewNew];
}
-(IBAction)btnClicked:(id)sender
{
[self.view addSubview:myView];
}