2013-01-24 36 views
1

如何找出鍵盤在iOS中第一次打開的時間?我只想知道單擊單元格(包含UITextField)並打開鍵盤的時間。之後,我使用包含上一個和下一個按鈕的工具欄瀏覽UITextFields。如何找出鍵盤第一次打開的時間? iphone

使用以下代碼,在瀏覽UITextFields時調用keyboardWillShow,即使在第一次單擊UITextField後鍵盤似乎仍保持打開狀態。這種方法並不能幫助我達到我的目的。

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

- (void)keyboardWillShow:(NSNotification *)notification { 

    } 

回答

1

使用此Notification..It可以幫助你

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

而在這keyboardWillShow方法去除觀察者這樣。

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
+0

謝謝穆拉利的回答:) –

+0

我試圖解釋我的問題更好,因爲第一次是有點模糊。 –

+0

只需在keyboardWillShow方法中刪除該Observer即可。因此,KeyboardWillShow將不會再次調用。 – Murali

1

添加到您的-viewDidLoad

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

然後,該方法keyboardWillShow:是這樣的:

- (void)keyboardWillShow:(NSNotification *)notification { 
    if (firstOpen) { 
    //do your stuff 
    firstOpen = NO; 
    } else { 
    //do smth else 
    } 
} 
+0

謝謝。這是我正在尋找的答案。:)但是我必須等5分鐘才能接受它。 –

+0

在第一次之後調用這個函數是沒有必要的......所以我們可以刪除觀察者... – Murali

+0

@Novarg我編輯了這個問題,在開始的時候我沒有解釋得很好。 –

相關問題