iosswiftmapkitmkmapviewpushviewcontroller

How to Move to 2nd Viewcontroller when we tap on mapView using MKMapView in swift?


In first viewcontroller i have mapview and below textfields containerview.. if i tap on mapview how to go second viewcontroller?

if i tap anywhere in map i should go to 2nd VC:

FirstVC.. if i tap on map i have to go to 2ndVC

2ndVC design:

2ndVC

for both viewcontrollers i am using MKMapView

for that i have written code from this answer Tap gesture for MapKit

override func viewWillAppear(_ animated: Bool) {

    let gestureRecognizer = UITapGestureRecognizer(target: self, action:"triggerTouchAction:")
     mapView.addGestureRecognizer(gestureRecognizer)

}
func triggerTouchAction(gestureReconizer: UITapGestureRecognizer) {
         //Add alert to show it works
       print("print working")
       let viewController = self.storyboard?.instantiateViewController(withIdentifier: "NewZoomAddressViewController") as! NewZoomAddressViewController;

      self.navigationController?.pushViewController(viewController, animated: true);
   }

then it hits below error

RegistrationViewController triggerTouchAction:]: unrecognized selector sent to instance 0x7f999e81a400 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Project.RegistrationViewController1 triggerTouchAction:]: unrecognized selector sent to instance 0x7f999e81a400'

How to design like above mentioned screens, please help.


Solution

  • Use UITapGestureRecognizer like this

    override func viewWillAppear(_ animated: Bool) {
    
            let gestureRecognizer = UITapGestureRecognizer(target: self, action:#selector(triggerTouchAction(_:)))
             mapView.addGestureRecognizer(gestureRecognizer)
    
    }
    @objc func triggerTouchAction(gestureReconizer: UITapGestureRecognizer) {
             //Add alert to show it works
           print("print working")
           let viewController = self.storyboard?.instantiateViewController(withIdentifier: "NewZoomAddressViewController") as! NewZoomAddressViewController;
    
          self.navigationController?.pushViewController(viewController, animated: true);
       }