How can I dismiss the keyboard programmatically?-Collection of common programming errors

I assume you mean you want to know which UITextField is the first responder (which is the text field that gets input from the keyboard).

There is no public API for this (though there is a private API). You can track which text field is the first responder manually using the textFieldDidBeginEditing: method of each text field’s delegate, or you can use a little trickery to find the first responder at any time.

Here’s the trick. The UIApplication object knows which object is the first responder, and can send a message to it. So you write a category like this on UIResponder:

UIResponder+firstResponderHack.h

#import 

@interface UIResponder (firstResponderHack)

+ (UIResponder *)firstResponderByHack;

@end

UIResponder+firstResponderHack.m

#import "UIResponder+firstResponderHack.h"

@interface FirstResponderFinder : NSObject
@property (strong, nonatomic) UIResponder *firstResponder;
@end

@implementation FirstResponderFinder
@synthesize firstResponder = _firstResponder;
@end

@implementation UIResponder (firstResponderHack)

- (void)putFirstResponderIntoFinder:(FirstResponderFinder *)finder {
    if (self.isFirstResponder)
        finder.firstResponder = self;
}

+ (UIResponder *)firstResponderByHack {
    FirstResponderFinder *finder = [FirstResponderFinder new];
    // Sending an action to nil sends it to the first responder.
    [[UIApplication sharedApplication] sendAction:@selector(putFirstResponderIntoFinder:) to:nil from:finder forEvent:nil];
    return finder.firstResponder;
}

@end

Then you can find the first responder, and check whether it’s a UITextField, like this:

UIResponder *firstResponder = [UIResponder firstResponderByHack];
if (firstResponder && [firstResponder isKindOfClass:[UITextField class]]) {
    UITextField *textField = (UITextField *)firstResponder;
    // do something with textField
}