isKindOfClass抓取标签

时间:2012-04-26 06:23:37

标签: ios xcode uiscrollview tags uibutton

我只想在ScrollView中抓取按钮标签1~5,其他数据不需要掌握

(void)layoutScrollImages
{
    UIImageView *view = nil;
    NSArray *subviews = [weekscroll subviews];

    CGFloat curXLoc = 0;
    for (view in subviews)
    {
        if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
        {
            NSLog(@"%d",view.tag);
            CGRect frame = view.frame;
            frame.origin = CGPointMake(curXLoc, 0);
            view.frame = frame;
            curXLoc += (wScrollObjWidth);
        }   
    }
    NSLog(@"subviews = %@",subviews);
    [weekscroll setContentSize:CGSizeMake((wNumImages * wScrollObjWidth), [weekscroll bounds].size.height)];
}

    subviews = (
    "<UIImageView: 0x1ba330; frame = (313 61; 7 5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x1bac00>>",
    "<UIImageView: 0x1bc900; frame = (315 59; 5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x1bdde0>>",
    "<UIButton: 0x1bd380; frame = (0 1; 50 66); opaque = NO; layer = <CALayer: 0x1bd410>>",
    "<UIButton: 0x1bd690; frame = (50 0; 50 66); opaque = NO; layer = <CALayer: 0x1bd720>>",
    "<UIButton: 0x1bd9a0; frame = (100 0; 50 66); opaque = NO; layer = <CALayer: 0x1bda30>>",
    "<UIButton: 0x1bc570; frame = (150 0; 50 66); opaque = NO; layer = <CALayer: 0x1bc600>>",
    "<UIButton: 0x1beaa0; frame = (300 0; 50 66); opaque = NO; tag = 1; layer = <CALayer: 0x1beb30>>",
    "<UIButton: 0x1bff40; frame = (350 0; 50 66); opaque = NO; tag = 2; layer = <CALayer: 0x1c0920>>",
    "<UIButton: 0x1c0e60; frame = (400 0; 50 66); opaque = NO; tag = 3; layer = <CALayer: 0x1c0ef0>>",
    "<UIButton: 0x1c1430; frame = (450 0; 50 66); opaque = NO; tag = 4; layer = <CALayer: 0x1c14c0>>",
    "<UIButton: 0x1c1a00; frame = (500 0; 50 66); opaque = NO; tag = 5; layer = <CALayer: 0x1c1a90>>",

    "<UIButton: 0x1cb340; frame = (1800 0; 50 66); opaque = NO; layer = <CALayer: 0x1cb3d0>>",
"
)UIImageView: 0x1ba330; frame = (313 61; 7 5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = 

2 个答案:

答案 0 :(得分:1)

根据打印日志,只有UIButton有标签,而在示例代码中你正在使用UIImageView,所以根据你的要求使用给定的代码。

我已经修改了您的代码,只需检查一下。

for (id objView in subviews)
{
    if ([objView isKindOfClass:[UIImageView class]])
    {
        UIImageView *imgView = (UIImageView*) objView;
        if (imgView.tag > 0)
        {
            NSLog(@"%d",imgView.tag);
            CGRect frame = imgView.frame;
            frame.origin = CGPointMake(curXLoc, 0);
            imgView.frame = frame;
            curXLoc += (wScrollObjWidth);
        }
    }   
}

或者如果你只想抓取UIButtons,

for (UIButton *btnView in subviews)
{
    if ([btnView isKindOfClass:[UIButton class]] && btnView.tag > 0)
    {
        NSLog(@"%d",btnView.tag);
        CGRect frame = btnView.frame;
        frame.origin = CGPointMake(curXLoc, 0);
        btnView.frame = frame;
        curXLoc += (wScrollObjWidth);
    }   
}

答案 1 :(得分:0)

你想要标签大于0的按钮..那你为什么要检查[UIImageView类] ..?

您要查找标签大于0的按钮的条件应为:

if ([aView isKindOfClass:[UIButton class] && (aView.tag > 0)]) {
}