Turning user input into button label

Asked

Viewed 709 times

3

Guys, I’m trying to create a button that when pressed enables a text field that should take the user input and change the button label to input, but when I tried to play only got the following result: Result Video

My Viewcontroller:

import Uikit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var textFieldInput: UITextField!
    @IBOutlet weak var iphoneSaveCharName: UIButton!
    @IBOutlet weak var charOne: UIButton!
    @IBOutlet weak var charTwo: UIButton!
    @IBOutlet weak var charThree: UIButton!
    @IBOutlet weak var charFour: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    var savedText: String!

//    func textFieldShouldReturn(textFieldInput: UITextField) -> Bool {
//        savedText = textFieldInput.text
//        textFieldInput.resignFirstResponder()
//        return false
//    }

    func textFieldEditCompleted(textFieldInput: UITextField) -> Bool
    {
        savedText = textFieldInput.text
        textFieldInput.resignFirstResponder()
        return false
    }

    func textFieldShouldReturn(textFieldInput: UITextField) -> Bool
    {
        return textFieldEditCompleted(textFieldInput);
    }


    @IBAction func editText(sender: AnyObject) {
        if sender is UILongPressGestureRecognizer &&
            sender.state == UIGestureRecognizerState.Began {

                textFieldInput.hidden = false

                let button = sender.view as UIButton


                if button.tag == 1 {
                    charOne.setTitle(savedText, forState: .Normal)
                } else if button.tag == 2{
                    charTwo.setTitle(savedText, forState: .Normal)
                } else if button.tag == 3{
                    charThree.setTitle(savedText, forState: .Normal)
                } else if button.tag == 4{
                    charFour.setTitle(savedText, forState: .Normal)
                }
        }
    }

}

1 answer

3


I checked your code and I think the best approach would be to store the reference of the selected button. I made the code simpler to understand the steps.

The method textFieldShouldReturn is part of the UITextFieldDelegate where the fieldInput will pass. By calling the resignFirstResponder() will force the passage in the action didEditingEnd. Then update the button title.

Note: For test purposes, you first have to select a button because there is initially no reference to the selected button.

Has been tested:

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.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, forState: .Normal)
    }

    @IBAction func didTouchButton(sender: AnyObject) {
        // Acção associada a cada botão
        selectedButton = sender as UIButton
    }
}

EDITED TO USE UILongPressGestureRecognizer

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.hidden = 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, forState: .Normal)
        fieldInput.hidden = 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.hidden = false
                // Acção associada a cada botão
                selectedButton = button
            }
        }
    }
}

Final result:

resultado

  • But then, the point is just edit when using a Long Press Gesture.

  • You only want to change the button title if you use a Long Press Gesture in Textfield, that’s it?

  • Not on the button, you will use the Long Press on the button, the text field that was invisible will be visible, you place the desired button title and when pressing Re-turn on the keyboard it saves on the button you clicked

  • I get it. It’s 4x UILongPressGestureRecognizer associated with each button and the action of UILongPressGestureRecognizer is always the same method, right?

  • Right! That’s exactly it

  • It worked right, you’re a genius guy, thank you!!!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.