Change buton's Label when clicking and picking the typed value

0

Good afternoon, I have a button in my application that the value will change as the button is selected, it should open a field for the user to type and consequently he will get the value typed and change the label.

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {


@IBOutlet weak var fieldInput: UITextField! // Campo
@IBOutlet weak var charOne: UIButton!
@IBOutlet weak var charTwo: UIButton!
@IBOutlet weak var charThree: UIButton!
@IBOutlet weak var charFour: UIButton!

var selectedButton: UIButton!
var savedText: String!

override func viewDidLoad() {
    super.viewDidLoad()

    // Atribuir delegate ao campo
    fieldInput.isHidden = true
    fieldInput.delegate = self
}

func textFieldShouldReturn(textFieldInput: UITextField) -> Bool
{
    if (textFieldInput == self.fieldInput) {
        // Guardar o texto do campo
        savedText = textFieldInput.text
    }
    // Finalizar a edição
    textFieldInput.resignFirstResponder()
    return false;
}

@IBAction func didEditingEnd(sender: AnyObject) {
    // Acção associada ao campo
    selectedButton.setTitle(savedText, for: .normal)
    fieldInput.isHidden = true
}




//UILongPressGestureRecognizer associado a cada botão
@IBAction func didLongPressButton(sender: AnyObject) {
    if  sender is UILongPressGestureRecognizer &&
        sender.state == UIGestureRecognizerState.began {
        if let button = sender.view! as? UIButton {
            // Inicia alteração
            fieldInput.isHidden = false
            // Acção associada a cada botão
            selectedButton = button
        }
    }
}
}

It's complaining about the view, I do not know what's wrong

    
asked by anonymous 27.07.2017 / 20:33

1 answer

0

From what I saw here you are not using the correct signature of the textFieldShouldReturn method which is:

func textFieldShouldReturn(_ textField: UITextField) -> Bool

You can also use the textFieldDidEndEditing method of UITextFieldDelegate to get the value of the textField and set it as the title of the button.

I made some changes to your code and it worked correctly here. Below is my code, if you have any questions, please let me know when I have a little more time.

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var fieldInput: UITextField! // Campo
@IBOutlet weak var charOne: UIButton!
@IBOutlet weak var charTwo: UIButton!
@IBOutlet weak var charThree: UIButton!
@IBOutlet weak var charFour: UIButton!

var selectedButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    // Atribuir delegate ao campo
    fieldInput.isHidden = true
    fieldInput.delegate = self
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
    textField.resignFirstResponder()
    return false;
}

func textFieldDidEndEditing(_ textField: UITextField) {
    selectedButton.setTitle(textField.text, for: .normal)
    fieldInput.isHidden = true
    fieldInput.text = ""
}

//UILongPressGestureRecognizer associado a cada botão
func didLongPressButton(sender: Any) {
    guard let sender = (sender as? UILongPressGestureRecognizer) else {
        return
    }

    if sender.state == UIGestureRecognizerState.began {
        if let button = sender.view! as? UIButton {
            // Inicia alteração
            fieldInput.isHidden = false
            // Acção associada a cada botão
            selectedButton = button
        }
    }
}
}
    
02.08.2017 / 02:19