获取 Button 和 Textfield 数据,当它们在 swift 5 中以编程方式添加时
Posted
技术标签:
【中文标题】获取 Button 和 Textfield 数据,当它们在 swift 5 中以编程方式添加时【英文标题】:Get Button and Textfield data , when they are added programmatically in swift 5 【发布时间】:2021-10-26 08:48:09 【问题描述】:我有一个动态表单,我在其中基于 json 响应创建文本字段和按钮。按钮和文本字段的数量可能会有所不同。 当我点击按钮以显示带有选项列表的操作表(每个按钮的相应数据)或将数据放入文本字段时,我如何获取保存在其特定变量中的特定字段的数据?
//Buttons
func subViewButton(placeholder:String)
let myFirstButton = UIButton()
myFirstButton.setTitle(placeholder, for: .normal)
myFirstButton.setTitleColor(.blue, for: .normal)
myFirstButton.backgroundColor = UIColor.systemGray
myFirstButton.frame = CGRect(x: 0, y: 0, width: 1, height: 40)
myFirstButton.addTarget(self, action: #selector(pressed), for: .touchUpInside)
formStackView.addArrangedSubview(myFirstButton)
formStackView.translatesAutoresizingMaskIntoConstraints = false
@objc func pressed()
//action here for specific button press
//Textfields
func subviewTextField(placeholder:String)
let tectfield = SkyFloatingLabelTextField(frame: CGRect(x: 0, y: 0, width: 1, height: 40))
tectfield.placeholder = placeholder
tectfield.font = UIFont.systemFont(ofSize: 15)
tectfield.autocorrectionType = UITextAutocorrectionType.no
tectfield.clearButtonMode = UITextField.ViewMode.whileEditing
tectfield.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
tectfield.delegate = self
tectfield.translatesAutoresizingMaskIntoConstraints = false
tectfield.selectedTitleColor = Utility.GlobalVariable.yellowColor
tectfield.selectedLineColor = Utility.GlobalVariable.yellowColor
tectfield.tintColor = Utility.GlobalVariable.blueColor
formStackView.spacing = 8
formStackView.addArrangedSubview(tectfield)
formStackView.translatesAutoresizingMaskIntoConstraints = false
我有一个 for 循环,我根据一个键填充表单,其中定义它是按钮或文本字段。
【问题讨论】:
【参考方案1】:当您使用 for 循环创建所有按钮时,您可以枚举它们以便获取数据字段的索引并将索引作为标签分配给每个按钮。
一旦您点击按钮,它将触发 IBAction,该 IBAction 将按钮本身作为参数(发送方)提供。然后,您现在可以从其标签中发现哪个按钮被点击,并使用后者来检索相关数据。
private let formFields = ["Name", "Age", "Gender"]
private func createButtonsFromDataArray()
for (index, formField) in formFields.enumerated()
let button = UIButton()
// [...]
button.tag = index
button.addTarget(self, action: #selector(pressed(sender:)), for: .touchUpInside)
// [...]
@objc func pressed(sender: UIButton)
print("The associated data to the button is \(formFields[sender.tag])")
关于文本字段,您可以执行相同的操作,只是保留对您在数组中创建的所有文本字段的引用。通过这种方式,您将能够检索每个文本字段的 textField.text
信息,然后创建一个 Encodable
结构,您可以通过上传/POST URLRequest 向该结构提供您想要发布的不同信息。
private var formTextFields: [UITextField] = []
private func createTextFieldFromDataArray()
for (index, formField) in formFields.enumerated()
let textField = UITextField()
// [...]
textField.tag = index // probably not necessary as the position in the array would provide the index but... depends on which solution you want to go with
formTextFields.append(textField)
// [...]
private func postFormData()
// here obviously you have multiple options
// But let's say the form fields are not dynamic you can create a struct, if not you would have to encode to `[String:Any]` and reinterpret that on the backend.
var dataToUpload: [String: Any] = []
for (index, formField) in formFields.enumerated()
dataToUpload[formField] = formTextFields[index].text
let encoder = JSONEncoder()
let data = encoder.encode(dataToUpload)
// URLSession.shared. etc.
如果不是动态结构:
struct FormData: Encodable
let age: Int
let name: String
let gender: String
【讨论】:
感谢您的解决方案。我还有一个疑问。如何在发布请求中发布这些值?我必须将它们保存在不同的变量中吗? @Bilal 我确实编辑了帖子,以便您有一个小想法,但问题的范围开始变得太宽了。所以请让我的帖子成为答案,然后再问一个缩小范围以适应政策的问题。很高兴为您提供帮助,不客气:)以上是关于获取 Button 和 Textfield 数据,当它们在 swift 5 中以编程方式添加时的主要内容,如果未能解决你的问题,请参考以下文章
unity中动态生成的Button和TextField重叠后,不能点击怎么办?
Swift:仅当键盘隐藏 TextField 或 Button 时滚动视图