在我开始解释之前,我只是几个月来用swift进行编码...好吧,所以,我希望能够以某种方式在第二个文本字段(键盘或Picker视图)上改变输入模式我输入了先前的文本字段。
因此,如果我在第一个文本字段中选择了数字2,则第二个文本字段将为选择器提供一个数组,该数组与数字2 [2.1、2.2、2.3 ...等]对应。
但是,如果我改为选择数字4,则第二个文本字段将向我显示键盘(而不是选择器视图),以便我可以在第二个文本字段上开始输入任何内容...
我试图通过对ViewDidLoad()方法使用if语句来做到这一点
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet weak var NumbersTF: UITextField!
@IBOutlet weak var SubnumbersTF: UITextField!
// for 1st pickerView
var numbers = ["1", "2", "3", "Other"]
//What I Want :
//
// Arrays for 2nd pickerview
var 1 = ["1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8"]
var 2 = ["2.1", "2.2", "2.3", "2.4", "2.5"]
var 3 = ["3.1", "3.2", "3.3", "3.4", "3.5"]
var pickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
NumbersTF.inputView = pickerView
NumbersTF.textAlignment = .center
NumbersTF.placeholder = "Select Number"
if NumbersTF.text == "4"{
SubnumbersTF.keyboardType.defalt
}
SubnumbersTF.placeholder = "Select Subnumber"
SubnumbersTF.textAlignment = .center
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if NumbersTF.isEditing {
NumbersTF.text = numbers[row]
NumbersTF.resignFirstResponder()
if NumbersTF.text == "4" {
SubnumbersTF.text = nil
SubnumbersTF.placeholder = "Please Enter New Number"
SubnumbersTF.keyboardAppearance = .default
}
}
else {
if SubnumbersTF.isEditing {
if NumbersTF.text == "1" {
SubnumbersTF.inputView = pickerView
SubnumbersTF.text = 1[row]
SubnumbersTF.resignFirstResponder()
}
if NumbersTF.text == "2" {
SubnumbersTF.inputView = pickerView
SubnumbersTF.text = 2[row]
SubnumbersTF.resignFirstResponder()
}
if NumbersTF.text == "3" {
SubnumbersTF.inputView = pickerView
SubnumbersTF.text = 3[row]
SubnumbersTF.resignFirstResponder()
}
}
}
}
有可能吗?