如何在 swift ios 中以编程方式添加多个文本字段
How to add multiple textfields programatically in swift ios
我试图通过 运行 循环显示多个 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)
}
}
错误与更正:
- 检查 self.view 的子视图项目内容 self.view.items 值打印在日志中。
- 您还没有为要分配给任何增量位置的 +1 计数器文本字段分配 X/Y 位置,因此即使创建了新对象,它也会与之前的相同项目重叠。
- 我总是建议在调用 addSubview 时同时将文本字段对象放入数组中,紧接在 self.view.addSubview(sampleTextField) 调用之后。这将帮助我在需要修改/访问/解除分配时保存每个文本字段项的引用。
- 您还为每个文本字段对象设置了标签,因此如果您不想使用数组,最好使用 [self.view viewWithTag:tagNo] 调用从子视图中获取带有该标签的对象.
您的代码段将如下所示
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.
}
}
print "array:textObjects.count 中的文本对象总数
如果需要从视图中获取对象,其 self.view.viewwithTag(tagNo) 将 return 精确的文本字段。
您只需添加 y 的动态值(如果需要,添加 x)。
var counter = 30
var yValue = 0
in for loop you just need to add below code
let sampleTextField = UITextField(frame: CGRect(x: 20, y: yValue, width: 300, height: 40))
yValue = Counter
//(space each textfield if 30 because height of textfield is 40)
counter = counter + 70
我试图通过 运行 循环显示多个 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)
}
}
错误与更正:
- 检查 self.view 的子视图项目内容 self.view.items 值打印在日志中。
- 您还没有为要分配给任何增量位置的 +1 计数器文本字段分配 X/Y 位置,因此即使创建了新对象,它也会与之前的相同项目重叠。
- 我总是建议在调用 addSubview 时同时将文本字段对象放入数组中,紧接在 self.view.addSubview(sampleTextField) 调用之后。这将帮助我在需要修改/访问/解除分配时保存每个文本字段项的引用。
- 您还为每个文本字段对象设置了标签,因此如果您不想使用数组,最好使用 [self.view viewWithTag:tagNo] 调用从子视图中获取带有该标签的对象.
您的代码段将如下所示
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.
}
}
print "array:textObjects.count 中的文本对象总数
如果需要从视图中获取对象,其 self.view.viewwithTag(tagNo) 将 return 精确的文本字段。
您只需添加 y 的动态值(如果需要,添加 x)。
var counter = 30
var yValue = 0
in for loop you just need to add below code
let sampleTextField = UITextField(frame: CGRect(x: 20, y: yValue, width: 300, height: 40))
yValue = Counter
//(space each textfield if 30 because height of textfield is 40)
counter = counter + 70