0

We want Lyft button touch event because I am working in analytics, so, I need how many people choose Lyft but I can't put UIView click event. I try below code.

let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction))
cell.lyftButton.addGestureRecognizer(gesture)

How can i achieve this?

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
ZED
  • 1

2 Answers2

0

You can directly assign a selector method to lyftButton e.g

lyftButton.addTarget(self, action: #selector(lyftButtonAction(_:)), for: .touchUpInside)

@objc
func lyftButtonAction(_sender: UIButton) {
//Do your action
}
Najeeb ur Rehman
  • 403
  • 4
  • 11
  • Bro Thanks for reply but lyft is not a UIButton it is UIView Controller so cant use addTarget https://github.com/lyft/Lyft-iOS-sdk#lyft-button – ZED May 23 '19 at 12:50
  • Is your action method not get called when tapped on view ? – Najeeb ur Rehman May 23 '19 at 13:19
  • Somewhere inside the LyftButton UIViewController there's either a gesture recognizer or a control. You should inspect the view, figure out how it catches taps, and add your view as a target to that item. – Oscar Apeland May 23 '19 at 17:30
0

To retrieve the LyftButton, you'll need to fetch the button inside the Lyft view, after retrieving it, I tried to add another target to it which was your 'checkAction' method, but for some reason it is not being called. One workaround solution is:

  • On Auto Layout, created a transparent button on top of the Lyft Button View, let's callet it 'Transparent Lyft Button': Example (I've embeded in another view because it was on a stackView);
  • On the code, retrieved the button with the above method, held it in a variable, let's call it 'requestLyftButton' and disabled it.
  • Created an IBAction for the 'Transparent Lyft Button' that triggers the method 'self.checkAction' that you've created and also calls requestLyftButton.sendActions(for: .touchUpInside), which triggers the original Lyft SDK action.

To Retrieve Lyft UIButton:

@IBOutlet weak var lyftButton: LyftButton!
@IBOutlet weak var transparentLyftButton: UIButton!
var requestLyftButton: UIButton?

func retrieveLyftButton(in view: UIView) {
    for view in view.subviews {
        if let lyftBtn = view as? UIButton {
            lyftBtn.isEnabled = false
            requestLyftButton = lyftBtn
        } else {
            retrieveLyftBtn(in: view)
        }
    }
}

transparentLyftButton IBAction to trigger your method + lyft sdk original action:

@IBAction func requestLyft(_ sender: UIButton) {
    if let lyftBtn = requestLyftButton {
        checkAction() // Your method
        lyftBtn.sendActions(for: .touchUpInside)
    }
}

I hope that you can understand what was done, if you have any questions, just let me know.