当我们单击 iOS Swift 中的文本字段时,是否可以显示操作表和隐藏键盘?



当我单击文本字段时,我正在尝试从操作表中获取文本字段的值。我不想在单击文本字段时显示键盘。 实际上我需要 uitextfield 中的按钮单击属性,可以这样做吗?

试试这个应该适合你,首先为文本字段设置委托。

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
let actionSheet = UIActionSheet(title: "Select", delegate: self, cancelButtonTitle: "Yes", destructiveButtonTitle: "", otherButtonTitles: "A", "B")
actionSheet.actionSheetStyle = .default
actionSheet.show(in: self.view)
return false
}

textFieldShouldBeginEdit:

当用户执行以下操作时,文本字段将调用此方法 通常会启动文本字段文本的编辑。

使用 UITextFieldDelegate:

1-将委托添加到您的文本字段:

yourTextField.deleagete = self

2-像这样使用委托

func textFieldDidBeginEditing(textField: UITextField) {
textField.resignFirstResponder()
//call your function here
}

Swift 4

private var positionArray = ["All", "QB", "RB", "WR", "TE", "K", "DE", "OL", "LB", "EDGE", "DT", "CB", "S"]

1 向文本字段添加委托

tf.delegate = self

2 扩展你的 UITextfield 委托

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
// to avoid keyboard
textField.resignFirstResponder()
// add action sheet
let alert = UIAlertController(title: LocalString("draft_title"), message: LocalString("draft_select_position"), preferredStyle: .actionSheet)

for i in positionArray {
alert.addAction(UIAlertAction(title: i, style: .default , handler:{ (UIAlertAction)in
textfield.text = i
self.view2021Draft?.positionSelected(isSelected: true, positionValue: textfield.text ?? "")
}))
}

alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler:{ (UIAlertAction)in
self.view2021Draft?.positionSelected(isSelected: false, positionValue: textfield.text ?? "")
}))

//uncomment for iPad Support
alert.popoverPresentationController?.sourceView = self.view

self.present(alert, animated: true, completion: {
print("completion block")
})
return false
}

最新更新