I want to disable/enable all UIViews in a IBOutletCollection.
However the UIViews differ in class, so I can't call the setEnabled
directly.
Then I thought I would use the performSelector
method to do it, however I can only send an Object as parameter.
I read both on this site and on other sites that I could just use [NSNumber numberWithBool YES/NO]
, however the enabled state doesn't change when sending a NSNumber with either bool YES or NO.
I got the disabled part to work by using nil
, however I couldn't find a way to set it them enabled:
-(void) setControlsState: (BOOL) enabled
{
for(UIView *subview in controls)
{
NSNumber *boolObject = enabled? [NSNumber numberWithBool: YES]: nil;
if([subview respondsToSelector: @selector(setEnabled:)])
{
[subview performSelector: @selector(setEnabled:) withObject: boolObject];
}
else if([subview respondsToSelector: @selector(setEditable:)])
{
[subview performSelector: @selector(setEditable:) withObject: boolObject];
}
subview.alpha = enabled? 1: 0.5;
}
}
Where controls is a IBOutletCollection consisting of UISliders, UIButtons, UITextViews and UITextfields. (@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *controls;
)
Note: The UITextViews works fine wit the above code, it is only the other type of UIViews, which uses setEnabled
.