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)
}
}
}
}
But then, the point is just edit when using a Long Press Gesture.
– Skalwalker
You only want to change the button title if you use a Long Press Gesture in Textfield, that’s it?
– Ricardo Pereira
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
– Skalwalker
I get it. It’s 4x
UILongPressGestureRecognizer
associated with each button and the action ofUILongPressGestureRecognizer
is always the same method, right?– Ricardo Pereira
Right! That’s exactly it
– Skalwalker
It worked right, you’re a genius guy, thank you!!!
– Skalwalker