TextField

How do I add an action (like adding this text to an array) when I press the return button when in a text field? Also, how do I change the button from "return" to "done".

Answered by gph4ppy in 679195022

Please, next time, try to clarify the problem more - eg whether it applies to SwiftUI, UIKit, etc., because it's hard to guess. Below I present my solution with the usage of UIKit:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate  {
    // MARK: Properties
    var array: [String] = []
    let textField: UITextField = {
        let textField = UITextField(frame: CGRect(x: 5,
                                                  y: UIScreen.main.bounds.height / 2,
                                                  width: UIScreen.main.bounds.width - 10,
                                                  height: 50))

        textField.returnKeyType = .done // << This line changes return to the done key.

        textField.backgroundColor = UIColor.darkGray
        return textField
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set the delegate and add TextField to the subview
        textField.delegate = self
        view.addSubview(textField)
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // Add the TextField value to the array
        guard let value = textField.text else { return false }
        array.append(value)

        // Hide keyboard
        textField.resignFirstResponder()
        return true
    }
}

I'm not sure what exactly you searching for. This is the SwiftUI iOS 15 way:

TextField("Placeholder", text: $text, onCommit: {
     // Add Text to Array
 }).submitLabel(.done)
Accepted Answer

Please, next time, try to clarify the problem more - eg whether it applies to SwiftUI, UIKit, etc., because it's hard to guess. Below I present my solution with the usage of UIKit:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate  {
    // MARK: Properties
    var array: [String] = []
    let textField: UITextField = {
        let textField = UITextField(frame: CGRect(x: 5,
                                                  y: UIScreen.main.bounds.height / 2,
                                                  width: UIScreen.main.bounds.width - 10,
                                                  height: 50))

        textField.returnKeyType = .done // << This line changes return to the done key.

        textField.backgroundColor = UIColor.darkGray
        return textField
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set the delegate and add TextField to the subview
        textField.delegate = self
        view.addSubview(textField)
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // Add the TextField value to the array
        guard let value = textField.text else { return false }
        array.append(value)

        // Hide keyboard
        textField.resignFirstResponder()
        return true
    }
}
TextField
 
 
Q