可以聲明標籤特定視圖的值,並根據您的需要移除所有視圖:
*爲
{
如果(圖isKindOfClass:[UIView類])(*的UIView在[self.view子視圖]子視圖)
{
if (subview.tag == 101 || subview.tag == 102)
{
[subview removeFromSuperview];
}
}
} *
有此線下方的示例代碼:在由XIB創建 'BTN' 這個代碼按鈕和哪些調用btnClicked方法
- (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];
}
@Mobility您好會[[self.view子視圖] makeObjectsPerformSelector:@selector(removeFromSuperView)]就夠了?它來自我的頭頂,所以把它作爲sudo代碼而不是確切的代碼語法。 – bennythemink
@bennythemink我想從視圖層次結構中刪除所有視圖,而不僅僅是視圖上的子視圖。這項工作是否一樣? – iOSDev
嗨@Mobility如果你使用的是iOS5,你可以遍歷所有的子視圖控制器,將它們從它們的超級視圖和[subViewController removeFromParentViewController]中移除。這應該將它們從顯示器和層次結構中刪除 – bennythemink