3
\$\begingroup\$

I recently built a way to automagically dismiss firstResponders for an app I'm building. Is there a more efficient way to approach this?

- (void)resignFieldStackRespondersExcept:(id)fieldException
{
    for (id field in self.fieldStack) {
        if ([field isKindOfClass:[fieldException class]] ||
            ([[field class] isSubclassOfClass:[fieldException class]] || [[fieldException class] isSubclassOfClass:[field class]]) ||
            (([[field class] isSubclassOfClass:[UITextField class]] && [[fieldException class] isSubclassOfClass:[UITextView class]]) ||
             ([[field class] isSubclassOfClass:[UITextView class]] && [[fieldException class] isSubclassOfClass:[UITextField class]]))) {
            continue;
        }

        [self resignField:field];
    }
}

- (void)resignField:(id)field
{
    if ([field isKindOfClass:[AccessoryPicker class]]) {
        [field closePicker:YES];
    }

    if ([field isKindOfClass:[TagsTextView class]]) {
        [field saveTrailingTag];
    }

    if ([field isKindOfClass:[UITextField class]] || [field isKindOfClass:[UITextView class]]) {
        [field resignFirstResponder];
    }
}
\$\endgroup\$
  • \$\begingroup\$ It's a bit unclear what you're trying to do. Is this a UITextField class? What is this exactly? Where does this code reside? And are these methods part of the Foundation class you're subclassing and I should look them up in the APIs? If not, how are they called? This question needs some more detail. \$\endgroup\$ – nhgrif Jun 17 '14 at 0:19
3
\$\begingroup\$

There's not really enough information or context here to make any sort of comment on the efficiency of this pair of methods, but it seems like this could simply be narrowed down into a single method.

We're iterating through a collection so we can call resignField: on specific classes only, and all of the logic in resignField: already works on specific classes only. The only thing that's left is the check for whatever this exception is:

- (void)resignFieldStackRespondersExcept:(id)fieldException {
    for (id field in self.fieldStack) {
        if ([field isKindOfClass:[AccessoryPicker class]]) {
            [field closePicker:YES];
        } else if ([field isKindOfClass:[TagsTextView class]]) {
            [field saveTrailingTag];
        } else if ([field isKindOfClass:[UITextField class]] 
                   && ![[fieldException class] isSubclassOfClass:[UITextView class]]) {
            [field resignFirstResponder];
        } else if ([field isKindOfClass:[UITextView class]] 
                   && ![[fieldException class] isSubclassOfClass:[UITextField class]]) {
            [field resignFirstResponder];
        }
    }
}

Now we can eliminate the second method entirely. The first method is more readable and unnecessary checks have been removed.

\$\endgroup\$
  • \$\begingroup\$ My instinct tells me there are other significant improvements to be made with the code in question, but it requires a much deeper explanation of what's being done and some more context. I posted a comment to the original question 3 days before posting this answer in hopes of getting some of that, but did not, and as such, I have no intention of further updating this answer in response to updates to the question I feel are likely to come. At the time of this answer being post, I feel it is quite complete with respect to the question. \$\endgroup\$ – nhgrif Jun 20 '14 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.