Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting UIView Type

I keep seeing examples that manually iterate through all of the subviews of a certain type in a UIView. For example, if you want a click outside of a keyboard to dismiss the keyboard regardless of which field is active, you might:

-(IBAction)backgroundClick:(id)sender
{
    [myTextField resignFirstResponder];
    [myOtherTextField resignFirstResponder];
    // ... repeat for each of my zillion text fields.
}

Instead of something like:

for(UIView *v in self.view.subviews)
    if(v.hasKeyboard) // or something like java's instanceof
        [v resignFirstResponder];

Although improvements to the specific case of the keyboard (such as discovering which one is first responder now) are appreciated, I'm more interested in the general case.

like image 659
Peter DeWeese Avatar asked Feb 27 '23 08:02

Peter DeWeese


2 Answers

Couldn't you do something like

if([v isMemberOfClass:[UITextField class]]){
[v resignFirstResponder];
}

?

like image 91
Gubb Avatar answered Mar 08 '23 17:03

Gubb


Alternatively, you could iterate through the views with

if( [v respondsToSelector:@selector(isFirstResponder)] && [v isFirstResponder])
{
  [v resignFirstResponder];
  break;
}

This way will only call resignFirstResponder on the current first responder and will stop when the current first responder is found. It will work on UITextFields, UITextViews, etc as well as any future text editing views (assuming they continue to implement the responder methods).

For the more general case you can use

if( [v isKindOfClass:UITextField.class] )
{
  [v resignFirstResponder];
}

For generic keyboard dismissal the first is preferred, since all you are interested in is the keyboard. The object claims it will handle the message (method call), so you don't have to actually care what type it is. If you really need to be sure it is a UITextField the second option is preferred.

like image 37
Peter Avatar answered Mar 08 '23 16:03

Peter