Open ZevEisenberg opened 9 years ago
For easy reference, the code in question:
- (void)setSubviewsHighlighted:(BOOL)highlighted forView:(UIView *)view
{
[[view subviews] enumerateObjectsUsingBlock:^(UIView *subview, NSUInteger idx, BOOL *stop) {
if ([subview respondsToSelector:@selector(setHighlighted:)])
{
NSMethodSignature *ms = [subview methodSignatureForSelector:@selector(setHighlighted:)];
NSInvocation *iv = [NSInvocation invocationWithMethodSignature:ms];
[iv setTarget:subview];
[iv setSelector:@selector(setHighlighted:)];
BOOL hlt = highlighted;
[iv setArgument:&hlt atIndex:2];
[iv invoke];
}
// recursion!!
[self setSubviewsHighlighted:highlighted forView:subview];
}];
}
And the simpler version (also converted it to a class method, natch):
+ (void)setSubviewsHighlighted:(BOOL)highlighted forView:(UIView *)view
{
for ( UIView *subview in view.subviews ) {
if ( [subview respondsToSelector:@selector(setHighlighted:)] ) {
[(UIControl *)subview setHighlighted:highlighted];
}
// recursion!!
[self setSubviewsHighlighted:highlighted forView:subview];
}
}
This was me. I think the reason was that not everything that responds to setHighlighted:
is a UIControl
, for example UIImageView
. Also at the time I didn't know that performSelector:
could be used with primitives wrapped in NSNumber
or NSValue
, which is probably a more meaningful approach than casting to UIControl
, provided the ARC warning is squashed using the appropriate pragma
.
EDIT: UIImage
-> UIImageView
A simple method call with a cast to
(UIControl *)
will suffice. @onemahon did you write that originally? Do you remember what the reason for using an invocation was?