Reputation: 247
I need to have the app I am making have a keyboard pop up on a button long press.
I know how to recognize the long press but the problem is having the keyboard pop up and letting the keyboard know which var to edit.
Thanks for any help.
Edit: To be more specific I want the button to be a name so I would have:
p1s1PlayerName.setTitle(String(player1name), for UIControlState.normal)
and then I would have the long press pop up a keyboard. The keyboard would then need to edit
var player1name = "TextInputFromKeyboard"
full code that needs to be edited:
var player1name = "John"
func userLongPressed(_ sender: Any) {
print("user long pressed")
p1s1PlayerName.becomeFirstResponder()
}
@IBOutlet var p1s1PlayerName: UIButton!
@IBAction func p1s1LongPressPlayerName(_ sender: AnyObject) {
// This is where I don't know what to put to call to the function and have the keyboard pop up
}
Upvotes: 0
Views: 311
Reputation: 82759
initially add long gesture to your button like
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.longPress))
p1s1PlayerName.addGestureRecognizer(longPress)
and call method like
func userLongPressed(_ sender: Any) {
print("user long pressed")
yourTextfieldName.becomeFirstResponder()
}
for button single press
p1s1PlayerName.addTarget(self, action: #selector(self.userTapped), forControlEvents: .touchUpInside)
p1s1PlayerName.setTitle(String(player1name), for UIControlState.normal)
and action like
func userTapped(_ sender: Any) {
print("user tapped")
}
Upvotes: 1
Reputation: 36612
When you set up your long press, have the selector called include the following code.
textfieldToEdit.becomeFirstResponder()
If you want there to be some informational text for the user, just assign it to the placeholder.
textfieldToEdit.placeholder = "Enter name here"
Upvotes: 1