我想使用alert.textFields?.last?.text
和alert.textFields?.last?.text
函数引用两个以上的文本字段输入,但是我不确定如何引用第一个和最后一个之间的字段。
let alert = UIAlertController(title: "Insert Student", message: nil, preferredStyle: .alert)
alert.addTextField{ (tf) in tf.placeholder = "Name" }
alert.addTextField{ (tf) in tf.placeholder = "House" }
alert.addTextField{ (tf) in tf.placeholder = "Score" }
let action = UIAlertAction (title:"Submit", style: .default) { (_) in
guard let name = alert.textFields?.first?.text,
let house = alert.textFields?.last?.text,
let score = alert.textFields?.last?.text
答案 0 :(得分:1)
let alert = UIAlertController(title: "Insert Student", message: nil, preferredStyle: .alert)
alert.addTextField{ (tf) in
tf.placeholder = "Name"
tf.tag = 11
}
alert.addTextField{ (tf) in
tf.placeholder = "House"
tf.tag = 12
}
alert.addTextField{ (tf) in
tf.placeholder = "Score"
tf.tag = 13
}
let action = UIAlertAction (title:"Submit", style: .default) { (_) in
guard let nameTextField = alert.view.viewWithTag(11) as? UITextField,
let houseTextField = alert.view.viewWithTag(12) as? UITextField,
let scoreTextField = alert.view.viewWithTag(13) as? UITextField else { return }
guard let name = nameTextField.text,
let house = houseTextField.text,
let score = scoreTextField.text else { return }
}