ios10版本textView和textField注冊鍵盤通知

ios10版本textView和textField注冊鍵盤通知

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        print("Textfield獲得焦點(diǎn),點(diǎn)擊Return鍵")
        textField.resignFirstResponder()
        return true
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        
        if(text == "\n") {
            print("textview獲得焦點(diǎn),點(diǎn)擊return鍵")
            textView.resignFirstResponder()
            return false
        }
        return true
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        //注冊鍵盤出現(xiàn)通知
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow(_:)), name: UIResponder.keyboardDidShowNotification, object: nil)
        
        //注冊鍵盤隱藏通知
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide(_:)), name: UIResponder.keyboardDidHideNotification, object: nil)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
   
        //注銷鍵盤出現(xiàn)通知
        NotificationCenter.default.removeObserver(self,name:UIResponder.keyboardDidShowNotification,object:nil)
        
        //注銷鍵盤隱藏通知
        NotificationCenter.default.removeObserver(self,name:UIResponder.keyboardDidHideNotification,object:nil)

    }
    
    
    @objc func keyboardDidShow(_ notification: Notification){
        print("鍵盤打開")
    }
    
    @objc func keyboardDidHide(_ notification: Notification){
        print("鍵盤關(guān)閉")
    }
    
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,226評論 3 119
  • 北方的夏季,到了八九月份就不是特別熱了。 一早一晚涼了許多,而中午還是有點(diǎn)熱的。 沿著江邊的堤壩...
    哈爾濱王海軍閱讀 342評論 4 3
  • 生活總是這么的出點(diǎn)幺蛾子,就像那湖面的水波紋。 昨晚接到電話的那一刻,心里沒有想象的激動,甚至平靜的都比不上別的電...
    蘇子好閱讀 138評論 0 0
  • Q: Determine if a Sudoku is valid, according to: Sudoku P...
    wxqyppqm閱讀 499評論 0 0

友情鏈接更多精彩內(nèi)容