Search code examples
iosswiftuiviewviewcontrolleruitapgesturerecognizer

How to access a let statement in a UIView from my main View Controller?


Within the viewdidload in my my main ViewController I created a let statement so I can enable a tap feature.

override func viewDidLoad() {
    super.viewDidLoad()

    let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tap(_:)))
    self.view.addGestureRecognizer(tapRecognizer) }

In a separate view I'd like to disable this let statement:

tapRecognizer.isenabled = false

What's the easiest way to access the 'tapRecognizer' from my main ViewController without using a segue as the second view is a UIView so I don't think I can access it this way.


Solution

  • Declare tapRecognizer instance variable so it can be accessible in any method or out of the view Controller if it's currently presented

    var tapRecognizer:UITapGestureRecognizer!
    override func viewDidLoad() {
       super.viewDidLoad()
    
       tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tap(_:)))
       self.view.addGestureRecognizer(tapRecognizer) 
     }