2011-08-05 4 views
0

키보드가 작동하는지 어떻게 알 수 있습니까?iPhone에서 키보드 및 상태 얻기

첫 번째 응답자가되는 UISearchbar 인스턴스가 있습니다.

키보드가 나타나면 API의 일부로 알림이 전송되지만이 경우 바로 응답하지 않으려 고합니다. 내가 이것을 불리언 상태로 기록 할 수는 있지만 그것은 어색해 보입니다. 내가 알아낼 수있는 곳에서 "getter"가 있는지 알고 싶습니다.

답변

3

이는 내가 어떻게 :

KeyboardStateListener.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 
} 
+0

예 - 알림을 사용하여 찾을 수있는 유일한 옵션입니다. 하나의 코멘트 비록 : 관찰자와 함께 주어진 선택자는 일반적으로 단일 입력, 즉 알림을받습니다. 그래서'didShow :'와'- (void) didShow : (NSNotification *) aNotification; '등의 메소드를 사용하는 것이 더 나을 것입니다. 또한, 'UIKeyboardDisShowNotification'이'UIKeyboardWillShowNotification'보다 선호됩니다. – SK9

+0

@ SK9 : 네가 맞아. – Viraj

2

당신이 말한 것처럼 내가 할 수있는 유일한 확실한 방법. 이 같은 사용하여 통지 :

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillShow:) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

및보다 다음

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillHide:) 
               name:UIKeyboardWillHideNotification 
               object:nil]; 

다른, 당신은 귀하의 의견의 파단 반복과 같은 키보드를 찾을 수 있습니다 :

UIView *keyboard = nil; 

for (UIView *potentialKeyboard in [myWindow subviews]) { 

    // iOS 4 
    if ([[potentialKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) { 
     potentialKeyboard = [[potentialKeyboard subviews] objectAtIndex:0]; 
    }                     

    if ([[potentialKeyboard description] hasPrefix:@"<UIKeyboard"]) { 
     keyboard = potentialKeyboard; 
     break; 
    } 
    } 

그러나 SDK가 변경 될 때 이것이 깨지면 확실하지 않습니다 ...

아마도이 방법을 사용하여 창의 카테고리를 사용하면 항상 키보드에 대해 창문을 열어 볼 수 있습니다. 단지 생각 만 할 수 있습니다.

+0

을 고마워. UIKeyboardDownShowNotification을 관찰하는 것이 키보드가 올라 있는지 확인하는 것이 더 낫다는 것을 알았을 것입니다. – SK9

+0

물론 그렇습니다. 나는 당신이 이미 알림을 사용하고 도우미 클래스에 BOOL을 저장하는 방법을 이미 이해하고 있기 때문에 다른 방법을 제안했습니다. KeyboardStateListener의 싱글 톤은 멋진 터치입니다! – cdasher

관련 문제