如何确保在文本字段中仅选择选择器数据
how to ensure only picker data is selected in the textfield
我在 UITextfield 中使用以下代码作为选择器函数,我应该如何确保选择了数组中的值并且没有其他数据输入
我分享了下面的代码,我使用了“守卫”来检查字段是否为空
代码
var spinneCPickerData: [String] = ["1", "2", "3", "4", "5"]
@IBOutlet var pickerfield: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let spinnerCPicker = UIPickerView()
spinnerCPicker.delegate = self
pickerfield = spinnerCPicker
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return spinneCPickerData.count
}
func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return spinneCPickerData[row]
}
func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerfield.text = spinneCPickerData[row] as String
view.endEditing(true)
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// This method is triggered whenever the user makes a change to the picker selection.
// The parameter named row and component represents what was selected.
}
func setdata() {
guard let text data = PostCategoryField.text, !postcategory.isEmpty, else {
self.view.makeToast("Please Enter the Category!")
return
}
}
您需要将 inputView
设置为您的 pickerfield
。
像这样你可以做...
声明一个属性pickerView
private let pickerView = UIPickerView(frame: .init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 216))
在 viewDidLoad
方法中设置委托和数据源。
pickerView.dataSource = self
pickerView.delegate = self
pickerfield.inputView = pickerView
所以当 pickerfield
成为第一响应者时,选择器视图将代替键盘显示。
所以用户必须select来自选择器视图的值
更新
您可以 if-let
按照以下方式完成此操作。您需要将此代码放在要验证文本字段值的位置。
//When pickerfield.text value is one of the value of spinneCPickerData array
if let txt = pickerfield.text, spinneCPickerData.contains(txt) {
print("the textfield'd value is from the array")
}
我在 UITextfield 中使用以下代码作为选择器函数,我应该如何确保选择了数组中的值并且没有其他数据输入 我分享了下面的代码,我使用了“守卫”来检查字段是否为空 代码
var spinneCPickerData: [String] = ["1", "2", "3", "4", "5"]
@IBOutlet var pickerfield: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let spinnerCPicker = UIPickerView()
spinnerCPicker.delegate = self
pickerfield = spinnerCPicker
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return spinneCPickerData.count
}
func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return spinneCPickerData[row]
}
func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerfield.text = spinneCPickerData[row] as String
view.endEditing(true)
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// This method is triggered whenever the user makes a change to the picker selection.
// The parameter named row and component represents what was selected.
}
func setdata() {
guard let text data = PostCategoryField.text, !postcategory.isEmpty, else {
self.view.makeToast("Please Enter the Category!")
return
}
}
您需要将 inputView
设置为您的 pickerfield
。
像这样你可以做...
声明一个属性pickerView
private let pickerView = UIPickerView(frame: .init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 216))
在 viewDidLoad
方法中设置委托和数据源。
pickerView.dataSource = self
pickerView.delegate = self
pickerfield.inputView = pickerView
所以当 pickerfield
成为第一响应者时,选择器视图将代替键盘显示。
所以用户必须select来自选择器视图的值
更新
您可以 if-let
按照以下方式完成此操作。您需要将此代码放在要验证文本字段值的位置。
//When pickerfield.text value is one of the value of spinneCPickerData array
if let txt = pickerfield.text, spinneCPickerData.contains(txt) {
print("the textfield'd value is from the array")
}