判断虚拟键盘是否打开的代码

//第一种直接在textview的状态里判断 记得写textview的委托
- (void)textViewDidBeginEditing:(UITextView *)textView { bKeyBoardHide = NO; } - (void)textViewDidEndEditing:(UITextView *)textView { bKeyBoardHide = YES; }

 

第二种 键盘监控

[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject:nil];
 -(void)keyboardWillHide:(NSNotification *)notification
{
     NSLog(@"*-----HideKeyBoard");
     bKeyBoardHide = YES;
}

-(void)keyboardWillShow:(NSNotification *)notification
{
    NSLog(@"*-----ShowKeyBoard");
    bKeyBoardHide = NO;
}

/*
@implementation UIView (FindAndResignFirstResponder)

- (BOOL)findAndResignFirstResponder 

{
    if (self.isFirstResponder) 

    {
        [self resignFirstResponder];
       return YES;     
    }

   for (UIView* subView in self.subviews) 

    {
        if ([subView findAndResignFirstResponder])
       return YES;
    }
    return NO;
}

@end


//自动寻找并释放第一焦点
- (BOOL) TTIsKeyboardVisible

{
    // Operates on the assumption that the keyboard is visible if and only if there is a first
    // responder; i.e. a control responding to key events
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    return [window findAndResignFirstResponder];
}

*/

 

posted @ 2012-12-21 22:55  六界剑仙  阅读(6693)  评论(0)    收藏  举报