I install unit tests in my project to make sure that all UIViewController IBOutlets are connected to their respective Xib objects (ie, not zero after viewDidLoad.) I was considering applying a protocol to these UIViewControllers with the required function "getAllOutletNames", for example:
-(NSArray*)getAllOutletNames
{
return @[ @"outletproperty1", @"outletProperty2", ...];
}
... and then using [viewController valueForKey: outletName] to make sure none of them are null. The problem is that it is a little cumbersome; "getAllOutletNames" needs to be updated for each output added to xib, which can be easily overlooked. I would prefer to do this programmatically so that all the properties of IBOutlet can be automatically detected and repeated.
I read in this NSHipster article (cmd + f for attributes with an attribute) that the attribute applies to IBOutlets (or, an attribute supported by an attribute that I don't quite understand.)
It looks like I can get a list of all the properties in the class using part of this answer , and I can get their attributes using part of this answer . But, printing the attributes from IBOutlet and non-IBOutlet properties using the following code, I see no difference:
const char * type = property_getAttributes(class_getProperty([self class], [outletName UTF8String]));
NSString * typeString = [NSString stringWithUTF8String:type];
NSArray * attributes = [typeString componentsSeparatedByString:@","];
NSLog(@"%@",attributes);
IBOutlet
(
"T@\"UILabel\"",
"&",
N,
"V_titleLabel"
)
Non-iboutlet
(
"T@\"UIView\"",
"&",
N,
"V_programmaticallySetupView"
)
Is there a way to access this attribute, supported by the attribute that NSHipster mentioned in the article or otherwise determined whether the property is IBOutlet software programmatically, or am I taking the wrong tree here?