1

“if”条件在第一个循环中有效,但在第二个循环中无效我不明白为什么它不想在第二个循环中检查我的条件以及为什么它只在第一个循环中有效

for (UIView *view in dro.subviews) {
    for (TOJDropableButtonView *v in view.subviews) {
        if ([v.type isEqualToString:targetView.filterItem.searchFilterItem.type]){
            NSLog(@"%@", targetView.filterItem.searchFilterItem.type);
        }
    }
}


-[UIImageView type]: unrecognized selector sent to instance 0x161bb960
2012-12-11 20:18:00.985 HungryNow[2507:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView type]: unrecognized selector sent to instance 0x161bb960'
*** First throw call stack:
(0x1790012 0x149de7e 0x181b4bd 0x177fbbc 0x177f94e 0x1f401 0x2fe45 0x2ec6c 0x14b1705 0x4f2f4c 0x4f2fbc 0x41833f 0x418552 0x3f63aa 0x3e7cf8 0x27f2df9 0x27f2ad0 0x1705bf5 0x1705962 0x1736bb6 0x1735f44 0x1735e1b 0x27f17e3 0x27f1668 0x3e565c 0x24ad 0x23d5)
libc++abi.dylib: terminate called throwing an exception
4

1 回答 1

2

您正在将所有子视图投射到 TOJDropableButtonView。

但实际上,dro 有一些不属于 TOJDropableButtonView 类的子视图。dro 的子视图之一恰好是一个图像视图,它没有“类型”属性,您试图在 equalToString 行中访问它。

确保您感兴趣的子视图属于 TOJDropableButtonView 类,而不是将所有子视图类型转换为 TOJDropableButtonView。

下面的代码应该可以解决您的问题。

for (UIView *view in dro.subviews) {
    for (UIView *v in view.subviews) {
        if([v isKindOfClass:[TOJDropableButtonView class]]){
            if ([v.type isEqualToString:targetView.filterItem.searchFilterItem.type]){
               NSLog(@"%@", targetView.filterItem.searchFilterItem.type);
            }
        }
    }
}
于 2012-12-11T19:31:42.037 回答