您的位置:首页 > 移动开发 > IOS开发

ios 如何判断键盘是否已经显示

2015-01-08 13:15 507 查看
在群里看到有人问:ios如何判断键盘已经显示在界面上。

其实这个解决很简单:

写一个单例来管理键盘的状态。

这个单例在初始化方法init种监听2个事件,分别是

UIKeyboardDidShowNotification(键盘弹出通知)和

UIKeyboardWillHideNotification (键盘消失通知 然后在相应的方法中设置一个属性就行了。

大致的实现如下:

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

- (void)keyboardDidShow
{
_keyboardIsVisible = YES;
}

- (void)keyboardDidHide
{
_keyboardIsVisible = NO;
}

- (BOOL)keyboardIsVisible
{
return _keyboardIsVisible;
}

转载自:http://blog.sina.com.cn/s/blog_6531b9b80101c20u.html
参考:http://www.cnblogs.com/xinus/archive/2013/01/22/ios-keybord-notification.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: