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