2015-11-12 24 views
2

我的視圖控制器註冊到鍵盤通知(keyboardWillShow,keyboardWillHide)。iOS 9.0 - 獲取鍵盤將顯示/隱藏來自其他應用的通知

我啓動我的應用程序。它顯示了註冊到鍵盤通知的視圖控制器。鍵盤不可見。

我切換到短信應用程序,並開始寫文本。在寫作時,我的應用會收到通知。通知顯示爲屏幕頂部的橫幅。

當我點擊橫幅時,我的應用程序被打開並立即獲得鍵盤通知。

據我所知,此鍵盤通知與SMS的鍵盤有關。

如何識別鍵盤事件是否來自我的應用程序?

+0

一個問題,代表瞭解鍵盤通知?它們將僅限於您的應用內編輯活動。 – NSNoob

+0

在此處查看我的答案http://stackoverflow.com/a/40031687/2774520 –

回答

0

你可以刪除viewWillDisappear聽觀察員(鍵盤通知),並可開始viewWillAppear再次聽取觀察員,這可能你爲什麼不使用文本框/ TextView的解決問題,但

- (void)registerForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardWillShowNotification object:nil]; 

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

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(applicationWillEnterBackground:) 
               name:UIApplicationWillResignActiveNotification 
               object:nil]; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    [self registerForKeyboardNotifications]; 
} 

- (void)deregisterForKeyboardNotifications { 
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 
    [center removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [center removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
    [center removeObserver:self name:UIApplicationWillResignActiveNotification object:nil]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    [self deregisterForKeyboardNotifications]; 
}