3 回答

TA貢獻1936條經(jīng)驗 獲得超7個贊
…或采取簡單的方法:
輸入textField時,它成為第一響應(yīng)者,并出現(xiàn)鍵盤。您可以使用來檢查鍵盤的狀態(tài)[myTextField isFirstResponder]。如果返回YES,則鍵盤處于活動狀態(tài)。

TA貢獻1807條經(jīng)驗 獲得超9個贊
drawonward的代碼非常接近,但與UIKit的命名空間沖突,因此可以更易于使用。
@interface KeyboardStateListener : NSObject {
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

TA貢獻1851條經(jīng)驗 獲得超4個贊
UIKeyboardListener當(dāng)您知道鍵盤不可見時,請創(chuàng)建一個,例如通過[UIKeyboardListener shared]從調(diào)用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
- 3 回答
- 0 關(guān)注
- 538 瀏覽
添加回答
舉報