我对iOS编程有疑问。
我正在尝试从主UIView和子视图属性中获取所有子视图。 我正在使用委托“ - (void)listSubviewsOfView:(UIView *)view”,但此方法返回描述不佳的子视图信息。
例如:
- "UIImageView: 0x1d557250; frame = (10 6.5; 32 32); opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = CALayer: 0x1d557220",
- "UILabel: 0x1d557190; frame = (50 0; 240 43); text = '11:00'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = CALayer: 0x1d557960",
- “UIView:0x1d5568e0; frame =(110 10; 190 23); layer = CALayer:0x1d5568b0”,
但我希望得到,例如,UILabel文字颜色,字体大小和其他信息。 是否有某种方法或对象可以帮助我获取这些信息?
答案 0 :(得分:1)
#import <objc/runtime.h>
- (void)yourMethod{
UIView *parnetView = ...;
[parentView.subviews makeObjectsPerformSelector:@selector(printAllProperties)];
}
@interface UIView (printAllProperties)
- (void) printAllProperties;
@end
@implementation UIView (printAllProperties)
-(void)printAllProperties{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
unsigned int numberOfProperties = 0;
objc_property_t *propertyArray = class_copyPropertyList([self class], &numberOfProperties);
for (NSUInteger i = 0; i < numberOfProperties; i++) {
objc_property_t property = propertyArray[i];
NSString *name = [[NSString alloc] initWithUTF8String:property_getName(property)];
NSLog(@"Property %@ Value: %@", name, [self valueForKey:name]);
}
free(propertyArray);
});
}
@end
您需要将方法 printAllProperties 作为类别添加到UIView
答案 1 :(得分:0)
AFAIK在UIKit中没有可用的方法可以帮到你。您将不得不查询运行时系统以获取此信息。有关详细信息,请参阅this answer(如果您认为有用,请在链接中提供答案)。
要了解ObjC运行时,请仔细阅读Objective-C Runtime Programming Guide和Objective-C Runtime Reference。
希望有所帮助!