isKindOfClass 抓取标签

Posted

技术标签:

【中文标题】isKindOfClass 抓取标签【英文标题】:isKindOfClass grab tag 【发布时间】:2012-04-26 06:23:37 【问题描述】:

我只是想抓取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 = 

【问题讨论】:

我不明白实际的问题是什么。你能说得更具体些吗? NSArray *subviews = [weekscroll subviews];获取更多信息,我只想在ScrollView中抓取按钮标签1~5 我已经打印了子视图信息,我只想抓取按钮标签 【参考方案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);
       

【讨论】:

【参考方案2】:

您希望标签大于 0 的按钮正确.. 那您为什么要检查 [UIImageView 类].. ?

您找到标签大于 0 的按钮的条件应该是:

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

【讨论】:

以上是关于isKindOfClass 抓取标签的主要内容,如果未能解决你的问题,请参考以下文章

奇怪的`isKindOfClass`行为

🔥isKindOfClass 和 isMemberOfClass

isKindOfClass:和isMemberOfClass:-b

isKindOfClass和isMemberOfClass

OC中isKindOfClass与isMemberOfClass的区别

isKindOfClass和isMemberOfClass 的区别