iOS-回收鍵盤的幾種方法


在開發過程中,為了實現點擊屏幕其它位置收起鍵盤的目的,我們使用過許多的方法。
如果是在UIViewController中收起鍵盤,除了通過調用控件的resignFirstResponder方法外,還有其它的方法。

第一種方法

重載- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event方法,然后在此方法中執行[self.view endEditing:YES]。
代碼示例如下:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

如果獲取當前UIViewControll比較困難時,可以采用第二種或者第三種方法。直接執行以下兩個方法中的一個即可達到效果。

第二種方法

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

第三種方法

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];


免責聲明!

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



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