ioscocoa-touchuiresponderresponder-chain

Is there any way of asking an iOS view which of its children has first responder status?


In Mac OS X, you can find the first responder like this:

[[self window] firstResponder]

Is there any way of doing it in iOS? Or do you need to enumerate the child controls and send an isFirstRespondermessage to each one?


Solution

  • You would need to iterate over all of the child controls and test the isFirstResponder property. When you encounter TRUE, break out of the loop.

    UIView *firstResponder;
    for (UIView *view in self.view.subviews) //: caused error
    {
        if (view.isFirstResponder)
        {
            firstResponder = view;
            break;
        }
    }
    

    BETTER SOLUTION

    See Jakob's answer.