I set one UILongPressGestureRecognizer to handle four different buttons in my view, how do I access which button is being clicked in my code?
My UILongPressGestureRecognizer looks like it:
@IBAction func editText(sender: UILongPressGestureRecognizer) {
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
}
And I Want to use Long Press so that I can edit the button text
EDIT 1:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var iphoneTableView: UITableView!
@IBOutlet weak var textFieldInput: UITextField!
@IBOutlet weak var iphoneSaveCharName: UIButton!
@IBOutlet weak var charOne: UILabel!
@IBOutlet weak var charTwo: UILabel!
@IBOutlet weak var charTree: UILabel!
@IBOutlet weak var charFour: UILabel!
@IBOutlet weak var test1: UIButton! //button that I am clicking on!
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.
}
//Function I made so I can save the user input
@IBAction func iphoneSaveTextInput(sender: UIButton) {
let textData = textFieldInput.text
textFieldInput.hidden = true
iphoneSaveCharName.hidden = true
charTwo.text = textData
}
// This is the LongPress Action
@IBAction func editText(sender: UILongPressGestureRecognizer) {
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
func longPressMethod(gesture: UILongPressGestureRecognizer) {
println(gesture.view)
if gesture.view is UIButton {
let test1 = gesture.view as UIButton
println(test1)
}
}
}
}
Edit 2: Layout
Edit 3: New ViewController
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var iphoneTableView: UITableView!
@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.
}
@IBAction func iphoneSaveTextInput(sender: UIButton) -> Void{
let textData = textFieldInput.text
textFieldInput.hidden = true
iphoneSaveCharName.hidden = true
}
@IBAction func editText(sender: AnyObject) {
if sender is UILongPressGestureRecognizer &&
sender.state == UIGestureRecognizerState.Began {
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
// func iphoneSaveTextInput(sender: UIButton){
// var textData = textFieldInput.text
// textFieldInput.hidden = true
// iphoneSaveCharName.hidden = true
//
// }
let button = sender.view as UIButton
println(button)
if button.tag == 1{
charOne.setTitle("textData", forState: .Normal)
} else if button.tag == 2{
charTwo.setTitle("textData2", forState: .Normal)
} else if button.tag == 3{
charThree.setTitle("textData3", forState: .Normal)
} else if button.tag == 4{
charFour.setTitle("textData4", forState: .Normal)
}
}
}
}
Answer:
This is the final view control:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var iphoneTableView: UITableView!
@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.
}
@IBAction func iphoneSaveTextInput(sender: UIButton) -> Void{
let textData = textFieldInput.text
textFieldInput.hidden = true
iphoneSaveCharName.hidden = true
}
@IBAction func editText(sender: AnyObject) {
if sender is UILongPressGestureRecognizer &&
sender.state == UIGestureRecognizerState.Began {
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
// func iphoneSaveTextInput(sender: UIButton){
// var textData = textFieldInput.text
// textFieldInput.hidden = true
// iphoneSaveCharName.hidden = true
//
// }
let button = sender.view as UIButton
println(button)
if button.tag == 1{
charOne.setTitle("textData", forState: .Normal)
} else if button.tag == 2{
charTwo.setTitle("textData2", forState: .Normal)
} else if button.tag == 3{
charThree.setTitle("textData3", forState: .Normal)
} else if button.tag == 4{
charFour.setTitle("textData4", forState: .Normal)
}
}
}
}
I would mostly like to give you all a best answer since everybody helped me out! I had to create one Long Press for each button, otherwise code will get confused.
Your question has introduced me to a really cool feature, so thanks! :)
It turns out that if you attach a UILongPressGestureRecognizer to a UIButton in a storyboard and attach that button and gesture to an IBAction within your swift class, that IBAction method can recognize whether the touch is a tap or long press! Pretty cool, huh?
So first off, make sure that each UIButton has its own unique UILongPressGestureRecognizer; then you can edit your code like so, so that it's able to identify which button is being pressed and whether that press is either a simple tap or UILongPressGestureRecognizer:
// Connect both your button *and* its gestures to the
// `IBAction` method so that the function will be called
// no matter what kind of gesture it recognizes -- tap,
// long press, or otherwise.
@IBAction func buttonSelected(sender: AnyObject) {
// But to see if the gesture is a long press, you can
// simply check the sender's class and execute the code
// when the gesture begins.
if sender is UILongPressGestureRecognizer &&
sender.state == UIGestureRecognizerState.Began {
// These two lines are originally from your
// editText method
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
// Then to identify which button was long pressed you
// can check the sender's view, to see which button IBOutlet
// that gesture's view belongs to.
// Method #1:
let button = sender.view as UIButton
if button == thisButton {
} else if button == thatButton {
}
...
// Or you can check the gesture view's tag to see which
// button it belongs to (i.e. whichever button has a matching
// tag).
// Method #2:
let button = sender.view as UIButton
if button.tag == 1 {
} else if button.tag == 2 {
}
...
}
// Else if it's not a long press gesture, perform
// whatever action you'd like to accomplish during a
// normal button tap
else if !(sender is UILongPressGestureRecognizer) {
// These lines are originally from your
// iphoneSaveTextInput
let textData = textFieldInput.text
textFieldInput.hidden = true
iphoneSaveCharName.hidden = true
}
}
But if you want to continue to keep the UILongPressGestureRecognizer IBAction separate from the UIButton's IBAction (i.e. link the UIButtons to iphoneSaveTextInput: and your UILongPressGestureRecognizers to editText:), that should be OK too. Just keep your iphoneSaveTextInput: method as is, and update your editText: method like so:
// This is the LongPress Action
@IBAction func editText(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizerState.Began {
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
// Then to identify which button was long pressed you
// can check the sender's view, to see which button IBOutlet
// that gesture's view belongs to.
// Method #1:
let button = sender.view as UIButton
if button == thisButton {
} else if button == thatButton {
}
...
// Or you can check the gesture view's tag to see which
// button it belongs to (i.e. whichever button has a matching
// tag).
// Method #2:
let button = sender.view as UIButton
if button.tag == 1 {
} else if button.tag == 2 {
}
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With