从SuperView中删除按钮和标签?

时间:2014-01-27 13:56:44

标签: objective-c uiview ios7 uibutton

在.m文件中

@interface ReaderViewController ()
{
UIButton *button;
}

然后我创造 使用button对象

的两个按钮
  button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=-1;
[button addTarget:self
           action:@selector(record:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Rec/Stop" forState:UIControlStateNormal];
button.frame = CGRectMake(215, 110, 80, 50);
[self.view addSubview:button];

然后我再次使用button对象

创建一个按钮
     button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 button.tag=1;
 [button addTarget:self
       action:@selector(record:) forControlEvents:UIControlEventTouchUpInside];
 [button setTitle:@"Rec/Stop" forState:UIControlStateNormal];
 button.frame = CGRectMake(340, 110, 80, 50);
 [self.view addSubview:button];

现在我想从superView中删除这两个按钮? 我怎么能这样做

我已经尝试过这不起作用[button removeFromSuperview];

2 个答案:

答案 0 :(得分:0)

试试这个:

for (UIView* subV in self.view.subviews) {
        if ([subV isKindOfClass:[UIButton class]])
            [subV removeFromSuperview];
    }

注意:这会删除所有按钮进入超级视图

答案 1 :(得分:0)

为按钮设置有意义的标记值:

button.tag = 1000;

button.tag = 1001;

然后您可以删除按钮:

[[self.view viewWithTag:1000] removeFromSuperview];
[[self.view viewWithTag:1001] removeFromSuperview];

注意:您不需要实例变量button;你可以简单地使用局部变量。