Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform a segue by tapping a UIView

Tags:

ios

swift

segue

I wonder if it is possible to conenct a UIView property to a UIViewController. By connecting a UIView, I want to transition to a second view controller. I have attempted several times by dragging and coding, but I just faild. I am looking for ways that do not need to program like using seague.

Is there anything that achievs this goal?

Thanks

like image 232
Ryohei Avatar asked Oct 18 '16 20:10

Ryohei


People also ask

How do I segue to a view controller?

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.

How do I add tap gestures to a view?

Adding a Tap Gesture Recognizer in Interface Builder You don't need to switch between the code editor and Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.

How do I use UITapGestureRecognizer?

The iOS UITapGestureRecognizer class has a built-in way to detect a double tap on any view. All you need to do is create the recognizer, set its numberOfTapsRequired property to 2, then add it to the view you want to monitor.


2 Answers

Storyboard mode supports this (at least now.) Drag a Tap Gesture Recognizer onto your view you want to click. Then connect the gesture recognizer to the view you want to show, just like any other segue transition. Make sure any containing views are "User Interaction Enabled." No code required. Apple Documentation

like image 170
Ammo Goettsch Avatar answered Sep 20 '22 07:09

Ammo Goettsch


To perform a segue by tapping a UIView, you need to add a gesture recognizer. In my example I instantiate and added a Subclass of UIView programmatically :

viewDidLoad(){

    // here we instantiate an object of our subclass
    let customView = MyViewSubclass(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    // here we add it to our ViewController
    self.view.addSubview(customView)

    // here we instantiate an object of gesture recognizer
    let gestureRec = UITapGestureRecognizer(target: self, action:  #selector (self.someAction (_:)))
    // here we add it to our custom view
    customView.addGestureRecognizer(gestureRec)
}

func someAction(sender:UITapGestureRecognizer){     
   performSegueWithIdentifier("Whazzzzup", sender: self)
}

// Swift 3
func someAction(_ sender:UITapGestureRecognizer){  

   // this is the function that lets us perform the segue   
   performSegue(withIdentifier: "Whazzzup", sender: self)
}

If you don't have a Subclass of UIView, then you just add a UIView...

let customView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

Also you could of course just take your UIView Outlet and add the gesture recognizer to it.

let gestureRec = UITapGestureRecognizer(target: self, action:  #selector (self.someAction (_:)))
myView.addGestureRecognizer(gestureRec)

To present a ViewController without segue, you need to instantiate the ViewController:

func someAction(_ sender:UITapGestureRecognizer){
    let controller = storyboard?.instantiateViewController(withIdentifier: "someViewController")
    self.present(controller!, animated: true, completion: nil)
    // swift 2
    // self.presentViewController(controller, animated: true, completion: nil)
}

You need to set the withIdentifier in your ViewController's Attribute Inspector:

enter image description here

In this example withIdentifier would be: LandingVC

If you're using a UINavigationController and want a back Button, you push the ViewController on the Navigation stack:

self.navigationController?.pushViewController(controller!, animated: true)
like image 32
David Seek Avatar answered Sep 23 '22 07:09

David Seek