Frederic
Frederic

Reputation: 497

how to use label instead of button in prepareforsegue in swift

In my app you type an integer in a textField in viewcontroller1. If the integer is greater than 100 a label will pop up on screen and when you click on it, you will go to viewcontroller2. The same repeats from viewcontroller2 and viewcontroller3.

However I want the integer in viewcontroller1 to be displayed in another label in viewcontroller3, which I then learned to use prepareforsegue to be able to do so.

Now my problem is, my prepareforsegue only works if I use a button, its seems like I can't connect my label you click to go to the viewcontroller as a segue connection? (is this possible? If not, what can I do to keep my label but work as my button is now, so my prepareforsegue is working correctly.

Upvotes: 1

Views: 2121

Answers (2)

farhadf
farhadf

Reputation: 1978

A label is not a UIControl element so it doesn't recognize touch events natively. You need to attach a tapGestureRecognizer to the label (make sure user interaction is enabled on the label). Then you can attach the gesture recognizer to your @IBAction method or trigger the segue. If using IB, the referencing outlet connection from the gesture recognizer should be tied to the label.

Upvotes: 2

harsha yarabarla
harsha yarabarla

Reputation: 506

first give the segue an identifier in storyboard and on label click or tap method use performSegueWithIdentifier("identifier", sender: self). This way the prepareForSegue gets called even on tapping label (Note: userInteraction should be enabled on label) In your code replace self.presentViewController(vc as! UIViewController, animated: true, completion: nil) with self.performSegueWithIdentifier("identifier", sender: self)

Upvotes: 1

Related Questions