在開(kāi)發(fā)過(guò)程中,為了實(shí)現(xiàn)點(diǎn)擊屏幕其它位置收起鍵盤(pán)的目的,我們使用過(guò)許多的方法。如果是在UIViewController中收起鍵盤(pán),除了通過(guò)調(diào)用控件的resignFirstResponder方法外,還有其它的方法。
第一種方法
重載
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
方法,然后在此方法中執(zhí)行[self.view endEditing:YES]。
代碼示例如下:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
如果獲取當(dāng)前UIViewControll比較困難時(shí),可以采用第二種或者第三種方法。直接執(zhí)行以下兩個(gè)方法中的一個(gè)即可達(dá)到效果。
第二種方法
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
第三種方法
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];