![](/img/trans.png)
[英]UiITextView text is not deleting the text after tapping the delete button on the keyboard
[英]how to hide keyboard immediately after tapping on a text field?
我有一個自定義文本字段。 當用戶點擊它時,它會使用UITableViewController
打開一個彈出窗口。
我想阻止鍵盤彈出我的文本字段,看起來不可能。
所以我嘗試了下面的代碼,它在模擬器中工作,但它不適用於真正的iPhone!
@IBAction func provincePressed(_ sender: Any) {
(sender as AnyObject).resignFirstResponder()
self.view.endEditing(true)
怎么了? 我怎樣才能讓它適用於真正的iPhone呢? 或者可能阻止鍵盤出現!
嘗試在textFieldShouldBeginEditing方法中返回false
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
return false
}
事先將文本字段的isEnabled
為false
。
斯威夫特4
你可以在textFieldDidBeginEditing
方法中調用textField.resignFirstResponder()
如下所示:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField.tag == 1) {
//this is textfield 2, so call your method here
textField.resignFirstResponder()
}
}
您應該為想要的文本字段添加標記。 如果您希望ViewContriler中的所有文本字段具有相同的行為,則不需要該部分。
另外,不要忘記從UITextFieldDelegate
擴展ViewController並將textField委托給ViewController。
textField.delegate = self
這是根據SafoineMoncefAmine答案。
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == yourTextfield) {// No need tag Use QA Manager to handle the textfield
textField.resignFirstResponder()
// Open your menu here, add view, show the menu here.
}
}
除了這個答案,
extension MyViewController: UITextFieldDelegate {
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
if textField == YourTextField{
return false
}
else{
return true
}
}
}
您可以從UITextField
禁用用戶交互,並使用UIView
覆蓋它,並將uigesturerecognizer
添加到該視圖以觸發您的事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.