iphone - iPhone 上的键盘和起床状态

标签 iphone ios keyboard state

如何确定键盘是否已启动?

我有一个成为第一响应者的 UISearchbar 实例。

当键盘出现时,通知会作为 API 的一部分发出,但我不想立即对此做出回应。我可以在 bool 状态中记录它,但这看起来很笨重。我想知道是否有一些“ setter/getter ”,我可以打电话了解一下。

最佳答案

我是这样做的:

键盘状态监听器.h

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

KeyboardStateListener.m

#import "KeyboardStateListener.h"

static KeyboardStateListener *sharedObj;

@implementation KeyboardStateListener

+ (KeyboardStateListener *)sharedInstance
{
    return sharedObj;
}
+ (void)load
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    sharedObj = [[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;
}

-(void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

@end

然后用这个算出剩下的:

KeyboardStateListener *obj = [KeyboardStateListener sharedInstance];
if ([obj isVisible]) {
    //Keyboard is up
}

关于iphone - iPhone 上的键盘和起床状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6951201/

相关文章:

iphone - 在唯一的 NSIndexPath 和唯一的 NSUInteger(或 int)之间转换

iphone - 释放变量时出现段错误(EXC_BAD_ACCESS)

android - 软键盘的 POPUP 布局

jquery - UI 滚动像 iPhone HTML 5

ios - Xcode 6、iPhone 6 和 iPhone 4s 生成错误

ios - UIPrintPageRenderer 问题 : generate pdf when loading multiple css files in HTML file

ios - 文件管理器检查 fileExists 是否在 iOS 13 中不起作用

ios - iOS 如何确定默认启用哪些共享扩展?

android - 如何将常用电子邮件域(gmail/yahoo/hotmail)的快捷方式添加到 android 电视键盘?

Vim 和 azerty 键盘