Ios 是否从视图中删除所有UI按钮?
我很难从视图中删除所有UIButton 我已经在for循环中将它们添加到了Ios 是否从视图中删除所有UI按钮?,ios,objective-c,cocos2d-iphone,Ios,Objective C,Cocos2d Iphone,我很难从视图中删除所有UIButton 我已经在for循环中将它们添加到了UIScrollView,稍后我需要删除它们 for ( ... ; ... ; ...) { UIButton *button = .... // in your "add button loop" just record them in an array [self.transientViews addObject:button]; } // remove them later with f
UIScrollView
,稍后我需要删除它们
for ( ... ; ... ; ...) {
UIButton *button = ....
// in your "add button loop" just record them in an array
[self.transientViews addObject:button];
}
// remove them later with
for (UIView *view in self.transientViews)
[view removeFromSuperview];
[self.transientViews removeAllObjects];
所以要添加它们:(在cocos2d场景上)
我以后如何运行所有这些按钮并删除它们?
我没有链接到它们,我想在视图中的所有按钮上运行
编辑:尝试此操作未成功
for (int i=0; i<[assets count];i++)
{
UIButton *myButton = (UIButton *)[sview viewWithTag:i];
[((UIView *)myButton) removeFromSuperview];
}
for(inti=0;i虽然技术上可行,但这样设计代码不是一个好主意
我没有他们的链接
问题就在这里。在创建和添加它们时,将它们放入NSMutableArray
,然后迭代此数组以删除它们
但是,如果出于某种原因,您没有这样做,您可以检查视图的所有子视图是否为UIButton:
- (void)removeUIButtonsFromView:(UIView *v)
{
for (UIView *sub in v.subviews) {
if ([sub isKindOfClass:[UIButton class]]) {
[sub removeFromSuperview];
} else {
[self removeUIButtonsFromView:sub];
}
}
}
如果只有滚动视图中的按钮,请使用以下命令将其全部删除:
[sview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
您正在删除scrollviewsview
:为什么
添加按钮时,只需将它们添加到保留的NSArray
属性中即可。
然后,当您想要删除它们时,只需在该数组上迭代即可
//in your interface
@property (nonatomic, strong) NSArray *buttons;
//in your implementation
sview = [[UIScrollView alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
......
NSMutableArray *tempArray = [NSMutableArray array];
for(int i =0; i<[assets count]-1; i++)
{
UIImage *thumb= [assets objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[sview addSubview:button];
[tempArray addObject:button];
}
self.buttons = tempArray;
.......
// later, to remove all buttons
- (void) removeButtons
{
for(UiButton *button in self.buttons){
[button removeFromSuperview];
}
self.buttons = nil;
}
//在您的界面中
@属性(非原子、强)NSArray*按钮;
//在您的实现中
sview=[[UIScrollView alloc]
initWithFrame:[[UIScreen mainScreen]边界]];
......
NSMutableArray*tempArray=[NSMutableArray];
对于(int i=0;i有两种方法。一些人建议了一种方法。我个人喜欢将我添加的所有内容保留在一个NSMutableArray
(当您将它们添加到视图中时,将它们添加到数组中),然后在数组中循环以删除它们
for ( ... ; ... ; ...) {
UIButton *button = ....
// in your "add button loop" just record them in an array
[self.transientViews addObject:button];
}
// remove them later with
for (UIView *view in self.transientViews)
[view removeFromSuperview];
[self.transientViews removeAllObjects];
我喜欢这个,因为它给了我更多的灵活性。我可能想删除它们或其他东西。它们可以是UIView
的任何子类,我不必担心它。我尝试过在for循环中得到警告:uiscrollview上的集合表达式类型可能不会通过枚举来响应countByEnumerating…@user1994291“你搞砸了。注意,在Cocoa中使用缩写被认为是不好的风格。”NikolaiRuhe注意到,忽略了。(为了答案的可读性。)@H2CO3答案的可读性将通过使用view
而不是v
makeObjectsPerformSelector
这样的名称提高与其他代码相同的数量:)这可能不是“正确答案”对于OP,但这是一个很好的技巧!谢谢!@user1994291停止抱怨,努力理解你自己的代码,然后将代码添加到提供的一个地方,并以一种有意义的方式添加到一个地方。然后它就会起作用。我可以看到他们有一些不透明,我几乎看不到他们的背景…这里真的很糟糕…sview背景nd没有消失,但所有的按钮都消失了。我试图用[((UIView*)sview)removeFromSuperview]将他带走;这对buttons有效,但它仍然存在。我将其记录下来,以查看我与他的链接,并获取他的值。
[((UIView *)sview) removeFromSuperview]
//in your interface
@property (nonatomic, strong) NSArray *buttons;
//in your implementation
sview = [[UIScrollView alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
......
NSMutableArray *tempArray = [NSMutableArray array];
for(int i =0; i<[assets count]-1; i++)
{
UIImage *thumb= [assets objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[sview addSubview:button];
[tempArray addObject:button];
}
self.buttons = tempArray;
.......
// later, to remove all buttons
- (void) removeButtons
{
for(UiButton *button in self.buttons){
[button removeFromSuperview];
}
self.buttons = nil;
}
for ( ... ; ... ; ...) {
UIButton *button = ....
// in your "add button loop" just record them in an array
[self.transientViews addObject:button];
}
// remove them later with
for (UIView *view in self.transientViews)
[view removeFromSuperview];
[self.transientViews removeAllObjects];