动态文本字段创建和访问 Swift 中的那些字段 4
Dynamic textfield creation and access those field in Swift 4
for (index, value) in controlArr.enumerated() {
print("\(index): \(value)")
let yPosition : Int = index+1
DispatchQueue.main.async {
let categoryField = UITextField(frame: CGRect(x: 15, y: 84*yPosition+35, width: Int(self.view.frame.width - 35), height: 40))
// categoryField.placeholder = "Please Enter \(value)"
categoryField.font = UIFont.systemFont(ofSize: 15)
categoryField.text = "\(self.fieldArr[index])"
categoryField.delegate = self
categoryField.tag = 100 + index
// self.myDict["myTextfield\(index)"] = categoryField
categoryField.borderStyle = UITextBorderStyle.bezel
categoryField.autocorrectionType = UITextAutocorrectionType.no
categoryField.keyboardType = UIKeyboardType.default
categoryField.returnKeyType = UIReturnKeyType.done
categoryField.clearButtonMode = UITextFieldViewMode.whileEditing;
categoryField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
self.arrayOfTextFields.append(categoryField)
self.scrollView.addSubview(categoryField)
for item in self.controlArr {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 20))
if item == "dropdown"
{
categoryField.rightViewMode = UITextFieldViewMode.always
imageView.contentMode = .scaleAspectFit
let image = UIImage(named:"down.png")
imageView.image = image
categoryField.rightView = imageView
}else{
imageView.isHidden = true
}
}
}
如何获取创建的文本字段并用于访问这些字段。
我在 Dynamic
中尝试了很多实现
谢谢
您可以使用 UITextField
的 addTarget
方法并获取文本字段文本更改。
categoryField.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)
@objc func textFieldDidChange(_ textField: UITextField) {
print(textField.text ?? "")
}
获取所有textField文本
在单击按钮或某些地方调用下面的函数以获取 UITextField
的所有值
func alltextField() {
for sview in scrollView.subviews {
if sview is UITextField, let textField = sview as? UITextField {
print(textField.text ?? "")
}
}
}
不明白你为什么需要这个,但如果你不想在情节提要中多次创建文本字段,那么你可以轻松地在 table 视图中做到这一点。
在 table 视图中,根据需要获取一个文本字段和 return 数字,然后 return 它(table 视图方法中的行数)。您可以轻松地在(行索引路径的单元格)中的那些文本字段。
for (index, value) in controlArr.enumerated() {
print("\(index): \(value)")
let yPosition : Int = index+1
DispatchQueue.main.async {
let categoryField = UITextField(frame: CGRect(x: 15, y: 84*yPosition+35, width: Int(self.view.frame.width - 35), height: 40))
// categoryField.placeholder = "Please Enter \(value)"
categoryField.font = UIFont.systemFont(ofSize: 15)
categoryField.text = "\(self.fieldArr[index])"
categoryField.delegate = self
categoryField.tag = 100 + index
// self.myDict["myTextfield\(index)"] = categoryField
categoryField.borderStyle = UITextBorderStyle.bezel
categoryField.autocorrectionType = UITextAutocorrectionType.no
categoryField.keyboardType = UIKeyboardType.default
categoryField.returnKeyType = UIReturnKeyType.done
categoryField.clearButtonMode = UITextFieldViewMode.whileEditing;
categoryField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
self.arrayOfTextFields.append(categoryField)
self.scrollView.addSubview(categoryField)
for item in self.controlArr {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 20))
if item == "dropdown"
{
categoryField.rightViewMode = UITextFieldViewMode.always
imageView.contentMode = .scaleAspectFit
let image = UIImage(named:"down.png")
imageView.image = image
categoryField.rightView = imageView
}else{
imageView.isHidden = true
}
}
}
如何获取创建的文本字段并用于访问这些字段。
我在 Dynamic
中尝试了很多实现
谢谢
您可以使用 UITextField
的 addTarget
方法并获取文本字段文本更改。
categoryField.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)
@objc func textFieldDidChange(_ textField: UITextField) {
print(textField.text ?? "")
}
获取所有textField文本
在单击按钮或某些地方调用下面的函数以获取 UITextField
func alltextField() {
for sview in scrollView.subviews {
if sview is UITextField, let textField = sview as? UITextField {
print(textField.text ?? "")
}
}
}
不明白你为什么需要这个,但如果你不想在情节提要中多次创建文本字段,那么你可以轻松地在 table 视图中做到这一点。 在 table 视图中,根据需要获取一个文本字段和 return 数字,然后 return 它(table 视图方法中的行数)。您可以轻松地在(行索引路径的单元格)中的那些文本字段。