Rightpoint / RZUtils

Rightpoint Commonly Used Tools
Other
123 stars 22 forks source link

Don't use NSInvocation for RZButtonView #143

Open ZevEisenberg opened 9 years ago

ZevEisenberg commented 9 years ago

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?

ZevEisenberg commented 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];
    }];
}
ZevEisenberg commented 9 years ago

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];

    }
}
ndonald2 commented 9 years ago

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.

ndonald2 commented 9 years ago

EDIT: UIImage -> UIImageView