Iphone 从UIScrollView中删除按钮
我已经使用IB添加了一个UIScrollVIew。然后使用下面的代码向这个UIScrollVIew添加了几个按钮Iphone 从UIScrollView中删除按钮,iphone,objective-c,Iphone,Objective C,我已经使用IB添加了一个UIScrollVIew。然后使用下面的代码向这个UIScrollVIew添加了几个按钮 for (int index=0; index<3;index++) { UIButton* button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setTitle:buttonTitle forState:UIControlStateNormal]; [button set
for (int index=0; index<3;index++)
{
UIButton* button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:buttonTitle forState:UIControlStateNormal];
[button setFrame:CGRectMake(18,200,300,45)];
[button setTag:100+index];
[button addTarget:self action:@selector(butclick:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
}
for(int index=0;index在这些按钮上放置一个标记,然后您可以使用以下方法再次获取它们:
for (int index=0; index<3;index++)
{
UIButton *myButton = (UIButton *)[self.scrollView viewWithTag:100+i];
[myButton removeFromSuperView];
}
我正在键入相同的内容,但不知道viewWithTag如何回答所有添加的按钮。我将建议删除按钮的一系列标记。他是否需要在循环中调用此标记(以获取所有按钮)?我已通过添加TAG属性编辑了问题。我在上面添加的按钮数量可能会更改(可能是2、5、7或10)。我怎么知道每个按钮的标签才能移除它呢?数组的想法很有效,但听起来有点糟糕。另外一件事:我打赌提问者有一些应用程序条件,使他希望按钮被隐藏和重新显示,而不是移除和重新分配。这并不能解决标签问题,但它肯定会更漂亮把它们藏起来。是的。更好,但是现在,如果他需要任意数量的,编辑应该是“搜索”,通过计算他之前可能添加的最大数量,然后在viewWithTag出现子视图时删除。我会接受@Antonio的解决方案,但如果您不确定之前添加了多少按钮,请将其循环运行到可能存在的最大数量。在某些情况下,viewWithTag将显示为零,但发送没有任何害处ing[无从SuperView移除];
myButton.hidden = YES;