简体   繁体   中英

How to add multiple UITextField with UIButton action in swift

I am dragging UITextField after writing something but after dragging when click handleTextButton same textfield show up. So I am trying to add multiple UITextField with UIButton action.

Here is my code below:

lazy var editableTextField: UITextField = {
    let tf = UITextField()
    tf.isUserInteractionEnabled = true
    tf.translatesAutoresizingMaskIntoConstraints = false
    tf.returnKeyType = UIReturnKeyType.done
    return tf
}()

@objc func handleTextButton(){
    print("tetx button clicked")

    view.addSubview(editableTextField)
    //if editableTextField.returnKeyType = true {}

    //constraints func
    setupEditableTextButton()
}

override func viewDidLoad() {
    super.viewDidLoad()

    let gesture = UIPanGestureRecognizer(target: self, action: #selector(userDragged))
    editableTextField.addGestureRecognizer(gesture)
}

The point of lazy properties is that they are computed only when they are first needed, after which their value is saved.

If you are trying to add multiple textfield this will not work. As same textField instance is returned.

func editableTextField() -> UITextField {
    let tf = UITextField()
    tf.isUserInteractionEnabled = true
    tf.translatesAutoresizingMaskIntoConstraints = false
    tf.returnKeyType = UIReturnKeyType.done
    return tf
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM