Rah*_*yas 32 iphone xcode objective-c uiscrollview
如何从我的滚动视图中删除所有子视图...
我在滚动视图上面有一个uiview和一个按钮,就像这样....
这是我在滚动视图中添加子视图的代码
-(void)AddOneButton:(NSInteger)myButtonTag {
lastButtonNumber = lastButtonNumber + 1;
if ((lastButtonNumber == 1) || ((lastButtonNumber%2) == 1)) {
btnLeft = 8;}
else if ((lastButtonNumber == 2) || ((lastButtonNumber%2) == 0)) {
btnLeft = 162;
}
CGRect frame1 = CGRectMake(btnLeft, btnTop, 150, 150);
CGRect frame2 = CGRectMake(btnLeft, btnTop, 150, 150);
UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
Button.frame = frame1;
Button.tag = myButtonTag;
[Button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[Button setBackgroundColor:[UIColor clearColor]];
[Button setBackgroundImage:[UIImage imageNamed:@"WaitScreen.png"] forState:UIControlStateHighlighted];
    GraphThumbViewControllerobj = [[GraphThumbViewController alloc] initWithPageNumber:[[GraphIdArray objectAtIndex:myButtonTag]intValue]];
    GraphThumbViewControllerobj.view.frame=frame2;
    GraphThumbViewControllerobj.lblCounter.text=[NSString stringWithFormat:@"%d of %d",myButtonTag+1,flashCardsId.count];
    GraphThumbViewControllerobj.lblQuestion.text=[flashCardText objectAtIndex:myButtonTag];
    [myScrollView addSubview:GraphThumbViewControllerobj.view];
[myScrollView addSubview:Button];
if ((lastButtonNumber == 2) || ((lastButtonNumber%2) == 0)) {
btnTop = btnTop + 162;
}
if (btnTop+150 > myScrollView.frame.size.height) {
myScrollView.contentSize = CGSizeMake((myScrollView.frame.size.width), (btnTop+160));}
}
这是删除子视图的代码
if(myScrollView!=nil)
{
        while ([myScrollView.subviews count] > 0) {
            //NSLog(@"subviews Count=%d",[[myScrollView subviews]count]);
            [[[myScrollView subviews] objectAtIndex:0] removeFromSuperview];
}
alt text http://www.freeimagehosting.net/uploads/e5339a1f51.png
Tim*_*Tim 103
要从任何视图中删除所有子视图,您可以遍历子视图并发送每个removeFromSuperview调用:
// With some valid UIView *view:
for(UIView *subview in [view subviews]) {
    [subview removeFromSuperview];
}但这完全是无条件的,并将删除给定视图中的所有子视图.如果你想要更细粒度的东西,你可以采取以下几种不同的方法:
removeFromSuperview稍后以相同的方式向它们发送消息removeFromSuperview根据需要单独发送它们if在上面的循环中添加一个语句,检查类是否相等.例如,要仅删除视图中存在的所有UIButtons(或UIButton的自定义子类),您可以使用以下内容:// Again, valid UIView *view:
for(UIView *subview in [view subviews]) {
    if([subview isKindOfClass:[UIButton class]]) {
        [subview removeFromSuperview];
    } else {
        // Do nothing - not a UIButton or subclass instance
    }
}Wex*_*Wex 38
一个老问题; 但是因为这是谷歌的第一次打击,我想我也会记下这个方法:
[[myScrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
你不能用这个来检查isKindOfClass,但它仍然是一个很好的解决方案.
编辑:需要注意的另一点是滚动视图的滚动条被添加为该滚动视图的子视图.因此,如果您遍历滚动视图的所有子视图,您将遇到它.如果删除它会再次添加自己 - 但是如果你只是期望你自己的UIView子类在那里,那么知道这一点很重要.
Swift 3的修正案:
myScrollView.subviews.forEach { $0.removeFromSuperview() }
为了补充Tim所说的内容,我注意到你正在标记你的观点.如果要删除具有特定标记的视图,可以使用:
[[myScrollView viewWithTag:myButtonTag] removeFromSuperview];
我认为你不应该使用快速枚举建议.
for(UIView *subview in [view subviews]) {
   [subview removeFromSuperview];
}
如果您更改正在迭代的集合,这不应该抛出异常吗?http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocFastEnumeration.html#//apple_ref/doc/uid/TP30001163-CH18-SW3
这个例子可能更好.
NSArray *subviews = [[scroller subviews] copy];
for (UIView *subview in subviews) {
    [subview removeFromSuperview];
}
[subviews release];
| 归档时间: | 
 | 
| 查看次数: | 65179 次 | 
| 最近记录: |