Transforming user input into button label

3

I'm trying to create a button that when pressed enables a text field that should take the user's input and change the button's label to the input, however when I tried to reproduce I just got the following result: Video Outcome

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)
                }
        }
    }

}
    
asked by anonymous 19.12.2014 / 02:12

1 answer

3

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

The textFieldShouldReturn method is part of UITextFieldDelegate where fieldInput will pass. Calling resignFirstResponder() will force you to pass didEditingEnd . At this point update the button title.

Note: For test purposes, you must first select a button because there is 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:

    
19.12.2014 / 13:25