firstresponder 后,鍵盤不顯示


問題:調用了firstresponder ,但鍵盤並沒有顯示,必須手動點擊一下,才會顯示鍵盤

 

分析:原入口進入是好的,自己新加了個window,從這個winodw的界面中點擊進入就不顯示了。

說明界面本身並沒有問題,是自己新加的window導致了鍵盤彈不出

 

懷疑:

1、優先級

    排除,因為如果是優先級的問題,點擊觸發也不會彈出

2、自己新建的窗口的界面中,沒有釋放掉firstResponder,導致UITextField獲得不了

    將自己窗口的界面中,所有界面元素在界面willdisappear的時候都顯示調用一下resignFirstResponder,依然無效,排除

 

想不出別的原因了,google之,沒找到好的解決辦法,基本都是由於2,但看到了一些方法,如用

canbecomeFirstResponder來判斷當前view能否獲得firstResponder.

 

測試了下我的UITextField,果然返回no,為什么了?為什么這個view不能成為第一響應者,不應該啊!

https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/event_delivery_responder_chain/event_delivery_responder_chain.html#//apple_ref/doc/uid/TP40009541-CH4-SW1

看了下響應鏈,意外發現:

Note: Make sure that your app has established its object graph before assigning an object to be the first responder. For example, you typically call the becomeFirstResponder method in an override of the viewDidAppear: method. If you try to assign the first responder in viewWillAppear:, your object graph is not yet established, so the becomeFirstResponder method returns NO.

也就是說,我們應當最好在viewDidAppear中設置第一響應者。

 

最終發覺,它不能成為第一響應者,還是由於入口window的問題。

原因:我新建了一個window,把這個界面push進來,但是我並沒有把這個window設為keywindow,導致它在響應鏈的位置有變化,所以不能成為第一響應者。

解決:1、保存原來的keywindow   2、在這個新window顯示時,把它設為keyWindow   3、在它消失時,將原來的keywindow設回,ok.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM