Reputation: 926
I'm trying to send an action from a UIGestureRecognizer on a UIView class to its parent's View Controller.
HomeViewController contains a View that contains several (5) OperationViews.
OperationView extends UIView and looks like such:
class OperationView: UIView {
override func awakeFromNib() {
super.awakeFromNib()
// OperationView is on a view that has HomeViewController as it's controller in StoryBoard
let swipeGestureRecognizer = UISwipeGestureRecognizer(target: HomeViewController.self(), action: #selector(HomeViewController.operatorWasSwiped(_:)))
swipeGestureRecognizer.direction = .left
let tapGestureRecognizer = UITapGestureRecognizer(target: HomeViewController.self(), action: #selector(HomeViewController.operatorWasSwiped(_:)))
self.addGestureRecognizer(swipeGestureRecognizer)
self.addGestureRecognizer(tapGestureRecognizer)
}
And in HomeViewController I have this function that's supposed to be called by the UIGestureRecognizers:
@objc func operatorWasSwiped(_ sender : UIGestureRecognizer) {
if (sender.isKind(of: UITapGestureRecognizer.self)) { print("Tapped") }
else { print("Swiped") }
self.performSegue(withIdentifier: "segue1", sender: nil)
}
However, I don't get anything. Not even an error.
I've tried adding the UIGestureRecognizer to each OperationView using an IBOutlet on HomeViewController as such:
view1.swipeGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view2.swipeGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view3.swipeGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view4.swipeGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view5.swipeGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view1.tapGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view2.tapGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view3.tapGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view4.tapGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view5.tapGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view1.addGestureRecognizer(view1.swipeGestureRecognizer!)
view2.addGestureRecognizer(view2.swipeGestureRecognizer!)
view3.addGestureRecognizer(view3.swipeGestureRecognizer!)
view4.addGestureRecognizer(view4.swipeGestureRecognizer!)
view.addGestureRecognizer(view5.swipeGestureRecognizer!)
view1.addGestureRecognizer(view1.tapGestureRecognizer!)
view2.addGestureRecognizer(view2.tapGestureRecognizer!)
view3.addGestureRecognizer(view3.tapGestureRecognizer!)
view4.addGestureRecognizer(view4.tapGestureRecognizer!)
view5.addGestureRecognizer(view5.tapGestureRecognizer!)
Where I've added:
var swipeGestureRecognizer: UISwipeGestureRecognizer?
var tapGestureRecognizer: UITapGestureRecognizer?
To class variables for OperationView
And that works, but it's far from elegant (imagine if I had to add another view...kinda defeats the point of my previous abstraction)
Any ideas what's happening? Or what I'm missing?
I'm using Swift 4 and Xcode 9
Upvotes: 1
Views: 1151
Reputation: 951
You are creating that gesture inside of a method, and the gesture may only live in that scope while the function is called. So technically is disappearing before you use it. So creating the instance of that gesture outside of the method may live while the view exist. If that makes sense.
UPDATE: As pointed from the comments, we do need a current reference of the parent viewController.
How about doing it this way..
class OperationView: UIView {
let swipeGestureRecognizer = UISwipeGestureRecognizer()
let tapGestureRecognizer = UITapGestureRecognizer()
override func awakeFromNib() {
super.awakeFromNib()
if let parentViewController = parentViewController {
swipeGestureRecognizer.addTarget(parentViewController, action: #selector(HomeViewController.operatorWasSwiped(_:)))
swipeGestureRecognizer.direction = .left
tapGestureRecognizer.addTarget(parentViewController, action: #selector(HomeViewController.operatorWasSwiped(_:)))
self.addGestureRecognizer(swipeGestureRecognizer)
self.addGestureRecognizer(tapGestureRecognizer)
}
}
}
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}
Upvotes: 3