[英]iOS view subviews doesn't recognize custom class members
I subclass UIView and have an NSString member named imgName; 我子类化UIView并有一个名为imgName的NSString成员; All views added to the superview are MyCustomView; 添加到超级视图的所有视图均为MyCustomView;
I do this: 我这样做:
NSArray *arr = [view subviews];
for (int x = 0; x < [arr count]; x++)
{
MyCustomView *view = [arr objectAtIndex:x];
NSString *imgName = view.imgName; <-- Unrecognized selector
}
I really want access to that member. 我真的很想访问该成员。 If I kept a different running list of subviews image names it would be problematic because I would also have to maintain their positioning in the view hierarchy (as I want the view hierarchy as is with zIndexes). 如果我保留了一个不同的子视图图像名称运行列表,那将是有问题的,因为我还必须保持它们在视图层次结构中的位置(因为我希望像zIndexes一样保持视图层次结构)。
How can I get the string from [view subviews]? 如何从[view subviews]中获取字符串?
You should double check that the subview is of the correct type. 您应该仔细检查子视图的类型正确。 You can perform that check with something like this: 您可以使用以下方式执行该检查:
for ( int x = 0; x < [arr count]; ++x )
{
UIView *subView = [arr objectAtIndex:x];
if ( [subView class] == [MyCustomView class] )
{
//Perform actions on the view as needed
}
}
Odds are there is a subview that is not of your MyCustomView
type and when it is trying to cast and access that member, it cannot because it is not the appropriate type. 可能有一个子视图不是MyCustomView
类型的,并且当它试图强制转换和访问该成员时,它不能,因为它不是适当的类型。
EDIT: You did mention that all of your subviews added are MyCustomView
. 编辑:您确实提到您添加的所有子视图都是MyCustomView
。 I personally wouldn't trust the iOS structure to not have subview already embedded in a UIView
我个人不相信iOS结构不会在UIView
嵌入子视图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.