Adding a UIGestureRecognizer to Subprogrammatically Programmatically in Swift

I currently have a subview that is created and added to a UIView in ViewDidLoad() . I UIGestureRecognizers use UIGestureRecognizers to detect touch and display a specific button. My current code is:

  override func viewDidLoad() { super.viewDidLoad() architectView = CustomClass(frame: self.view.bounds) self.view.addSubview(architectView) let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:") gestureRecognizer.delegate = self architectView.addGestureRecognizer(gestureRecognizer) } func handleTap(gestureRecognizer: UIGestureRecognizer) { let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) } 

The handleTap() function is a simple test to see if handleTap() recognized. This code does not call UIAlert when clicked? What am I missing?

+8
ios iphone swift uiview uigesturerecognizer
source share
2 answers

I checked your code here and it works. However, I think you might be UIGestureRecognizerDelegate on adding the UIGestureRecognizerDelegate protocol to your view controller. See below:

 class ViewController: UIViewController, UIGestureRecognizerDelegate { var architectView = UIView() override func viewDidLoad() { super.viewDidLoad() architectView = UIView(frame: self.view.bounds) self.view.addSubview(architectView) let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:") gestureRecognizer.delegate = self architectView.addGestureRecognizer(gestureRecognizer) } func handleTap(gestureRecognizer: UIGestureRecognizer) { let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 
+12
source share

Swift 3 version code based on Rafael Silva's answer:

 import UIKit class ViewController: UIViewController, UIGestureRecognizerDelegate { var architectView = UIView() override func viewDidLoad() { super.viewDidLoad() architectView = UIView(frame: self.view.bounds) self.view.addSubview(architectView) let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(gestureRecognizer:))) gestureRecognizer.delegate = self architectView.addGestureRecognizer(gestureRecognizer) } func handleTap(gestureRecognizer: UIGestureRecognizer) { let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil)) self.present(alert, animated: true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 
+10
source share

All Articles