UILongPressGestureRecognizer accesses the pressed button
I have set up one UILongPressGestureRecognizer to handle four different buttons in my view, how do I access the button pressed in my code?
My UILongPressGestureRecognizer looks like this:
@IBAction func editText(sender: UILongPressGestureRecognizer) {
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
}
And I want to use Long Press to 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 last 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)
}
}
}
}
Basically I would like to give you a better answer since everyone helped me! I had to create one long press for each button, otherwise the code would be confusing.
source to share
Your question introduced me to a really interesting feature, so thanks! :)
It turns out that if you attach UILongPressGestureRecognizer
to UIButton
in the storyboard and attach this button and gesture to IBAction
in your swift class, this method IBAction
can recognize if a tap is a tap or a long press! Pretty cool, huh?
So, first of all, make sure each UIButton
has its own unique one UILongPressGestureRecognizer
; then you can edit your code so that it can identify which button is pressed and whether the click is a simple click, 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 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 view, to see which button IBOutlet
// that gesture 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 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 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 keep UILongPressGestureRecognizer
IBAction
separate from UIButton
IBAction
(i.e. link UIButton
to iphoneSaveTextInput:
and yours UILongPressGestureRecognizer
to editText:
), that should be fine too. Just keep your method iphoneSaveTextInput:
as it is and update your method editText:
like this:
// 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 view, to see which button IBOutlet
// that gesture 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 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 {
}
...
}
}
source to share
You don't want to use the same gesture recognizer in different views:
Can you connect the UIGestureRecognizer to multiple views?
The view
gesture recognizer property is probably what you want to test, but you want different gesture recognizers to display each view.
If you want, they can all be bound to the same selector, and you can access the next kind of recognizer this way.
source to share
The general way to create a button has different actions depending on how long you press it, looks like this (timer is a property),
@IBAction func buttonDown(sender: UIButton) { // connected to touchDown
timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "handleTimer", userInfo: sender, repeats: false)
}
@IBAction func buttonUp(sender: UIButton) { // connected to touchUpInside
if timer != nil {
timer.invalidate()
println("change view")
}
}
func handleTimer() {
var button = timer.userInfo as UIButton
timer = nil
button.setTitle("New Title", forState: .Normal)
}
source to share