如何确定键盘是否已启动?
我有一个成为第一响应者的 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/