gpt4 book ai didi

ios - 如何在 swift ios 中以编程方式添加多个文本字段

转载 作者:行者123 更新时间:2023-11-30 11:56:45 25 4
gpt4 key购买 nike

我试图通过运行循环来显示多个textFields,无论文本字段noTextField的数量是否大于零(这是一个输入值),它应该在 ViewController 上显示多个文本字段。

这是我有一个文本字段并打印多个 hello 但无法显示多个文本字段。

我有什么

enter image description here

我想要什么

enter image description here

            if self.noTextFields! > 0 {
for _ in 0..<self.noTextFields! {
// self.createForm()
print ("hello")


let sampleTextField = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
sampleTextField.placeholder = "Enter text here"
sampleTextField.font = UIFont.systemFont(ofSize: 15)
sampleTextField.borderStyle = UITextBorderStyle.roundedRect
sampleTextField.autocorrectionType = UITextAutocorrectionType.no
sampleTextField.keyboardType = UIKeyboardType.default
sampleTextField.returnKeyType = UIReturnKeyType.done
sampleTextField.clearButtonMode = UITextFieldViewMode.whileEditing;
sampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
sampleTextField.delegate = self as? UITextFieldDelegate
self.view.addSubview(sampleTextField)

}
}

最佳答案

错误和更正:

  1. 通过打印在日志中的 self.view.items 值来检查 self.view 的 SubView 项目内容。
  2. 您尚未为要分配给任何增量位置的 +1 计数器文本字段分配 X/Y 位置,因此即使创建了新对象,它也会重叠在前一个项目上。
  3. 我总是建议在执行 addSubview 调用时,在 self.view.addSubview(sampleTextField) 调用之后立即将文本字段对象放入数组中。这将帮助我在需要修改/访问/取消分配时保存每个文本字段项目的引用。
  4. 您还可以为每个文本字段对象设置标签,因此如果您不希望使用数组,最好使用 [self.view viewWithTag:tagNo] 调用从 subview 中获取带有该标签的对象。

您的代码片段看起来像

     var xValue = 0,yValue =0
var tagNo = 0
var textObjects = allocate new array.

if self.noTextFields! > 0 {
for _ in 0..<self.noTextFields! {
// self.createForm()
print ("hello")

let sampleTextField = UITextField(frame: CGRect(x: xValue, y: yValue, width: 300, height: 40)) //Note that I have set xValue and yValue
sampleTextField.placeholder = "Enter text here"
sampleTextField.font = UIFont.systemFont(ofSize: 15)
sampleTextField.borderStyle = UITextBorderStyle.roundedRect
sampleTextField.autocorrectionType = UITextAutocorrectionType.no
sampleTextField.keyboardType = UIKeyboardType.default
sampleTextField.returnKeyType = UIReturnKeyType.done
sampleTextField.clearButtonMode = UITextFieldViewMode.whileEditing;
sampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
sampleTextField.delegate = self as? UITextFieldDelegate
sampleTextField.tag = tagNo //Note this line for tagNo
self.view.addSubview(sampleTextField)
textObjects.addObject:sampleTextField //Note this line for adding textfield to array for reference to get total items added

yValue = yValue + sampleTextField.frame.size.height + 20; //Added yValue to place another text view at bottom of initial one and 20px buffer for gap.
}
}

打印“数组中的文本对象总数:textObjects.count

如果需要从 View 中获取对象,其 self.view.viewwithTag(tagNo) 将返回精确的文本字段。

关于ios - 如何在 swift ios 中以编程方式添加多个文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47727504/

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