views:

1533

answers:

3

Hi,

I need to check condition is keyboard appearing, in my iphone app.

like:

if(keyboardIsPresentOnWindow) {
    //Do action 1
}
else if (keyboardIsNotPresentOnWindow) {
    //Do action 2
}

So please help how to check this condition?

Thanks.

+4  A: 

I think you need to use the notifications that are provided about the keyboard:

From: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITextField_Class/Reference/UITextField.html

Keyboard Notifications

When the system shows or hides the keyboard, it posts several keyboard notifications. These notifications contain information about the keyboard, including its size, which you can use for calculations that involve moving views. Registering for these notifications is the only way to get some types of information about the keyboard. The system delivers the following notifications for keyboard-related events:

* UIKeyboardWillShowNotification
* UIKeyboardDidShowNotification
* UIKeyboardWillHideNotification
* UIKeyboardDidHideNotification

For more information about these notifications, see their descriptions in UIWindow Class Reference. For information about how to show and hide the keyboard, see Text and Web.

beggs
I checked these notification, but don't know how to check these notifications. If you could post some example,that would be very helpful.
Jitendra Singh
Have a look at NSNotificationCenter. You'll have to register for the notifications you're interested in. Don't forget to unregister when your application exits.
Thomas Müller
+5  A: 

create a UIKeyboardListener when you know the keyboard is not visible, for example by calling [UIKeyboardListener shared] from applicationDidFinishLaunching.

@implementation UIKeyboardListener

+ (UIKeyboardListener) shared {
 static UIKeyboardListener sListener; 
 if ( nil == sListener ) sListener = [[UIKeyboardListener alloc] init];

 return sListener;
}

-(id) init {
 self = [super init];

 if ( self ) {
  NSNotificationCenter  *center = [NSNotificationCenter defaultCenter];
  [center addObserver:self selector:@selector(noticeShowKeyboard:) name:UIKeyboardDidShowNotification object:nil];
  [center addObserver:self selector:@selector(noticeHideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
 }

 return self;
}

-(void) noticeShowKeyboard:(NSNotification *)inNotification {
 _visible = true;
}

-(void) noticeHideKeyboard:(NSNotification *)inNotification {
 _visible = false;
}

-(BOOL) isVisible {
 return _visible;
}

@end
Thanks...This helped me.
Jitendra Singh
+3  A: 

drawnonward's code is very close, but collides with UIKit's namespace and could be made easier to use.

@interface KeyboardStateListener {
    BOOL _isVisible;
}
+ (KeyboardStateListener *)sharedInstance;
@property (nonatomic, readonly, getter=isVisible) BOOL visible;
@end

static KeyboardStateListener *sharedInstance;

@implementation KeyboardStateListener

+ (KeyboardStateListener *)sharedInstance
{
    return sharedInstance;
}

+ (void)load
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    sharedInstance = [[self alloc] init];
    [pool release];
}

- (BOOL)isVisible
{
    return _isVisible;
}

- (void)didShow
{
    _isVisible = YES;
}

- (void)didHide
{
    _isVisible = NO;
}

- (id)init
{
    if ((self = [super init])) {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(didShow) name:UIKeyboardDidShowNotification object:nil];
        [center addObserver:self selector:@selector(didHide) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

@end
rpetrich
Why does it need its own pool?
Yar
`+load` is a special method called by the Objective-C runtime. It is called for each class after the app binary loads, but before the `main()` function is entered. There is no guarantee that an autorelease pool will be live.
rpetrich
Thanks! The code helps me to understand Objective-c more. BTW, is there a reason for choosing `UIKeyboardWillHideNotification` instead of `UIKeyboardDidHideNotification`?
ohho
No reason. "Will" notifications occur as something is about to happen. "Did" notifications occur after they've happened (usually after the animation has completed)
rpetrich
@rpetrich, this looks good, but I think you can delete the `+ load` function. Why does it need its own autorelease pool? I don't see `autorelease` being called anywhere. And, if we call `[UIKeyboardListener sharedInstance]` from `applicationDidFinishLaunching` as drawnonward suggests, the `main()` function will have already created one.
MattDiPasquale
MattDiPasquale: If the +load method is deleted, sharedInstance will never be initialized. Since there is no guarantee that an autorelease pool is active when the runtime invokes a +load method, wrapping all calls to system-provided classes is necessary in case they call autorelease.
rpetrich