gpt4 book ai didi

swift - 如何使用 swift 引用文本字段?

转载 作者:行者123 更新时间:2023-11-28 13:41:31 24 4
gpt4 key购买 nike

我想使用 alert.textFields?.last?.textalert.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

最佳答案

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 }
}

关于swift - 如何使用 swift 引用文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55970454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com