在.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];
答案 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
;你可以简单地使用局部变量。