Swift - UITextField以编程方式从for循环获取文本供以后使用

时间:2014-10-28 00:15:53

标签: loops for-loop text swift uitextfield

嗨我在函数示例中有for循环的UITextFields:

func textfieldsinfunc(){
 for....{
   //5 times for example
   var textField:UITextField = UITextField(frame: CGRectMake(addedNX, fieldHeight, 143, 30))
   textField.borderStyle = .RoundedRect
   self.scrollView.addSubview(textField)
 }
}

如何在函数外部获取这5个textField的文本?

1 个答案:

答案 0 :(得分:2)

您可以使用标记来识别UIView。

for index in 1...5 {
    //5 times for example
    var textField:UITextField = UITextField(frame: CGRectMake(10, 100, 143, 30))
    textField.borderStyle = .RoundedRect
    textField.tag = index
    self.scrollView.addSubview(textField)
}

并在函数

之外使用viewWithTag
let textField: UITextField = scrollView.viewWithTag(1) as UITextField