1

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.

Balaji Kondalrayal
  • 1,743
  • 3
  • 19
  • 38
Frederic
  • 497
  • 2
  • 9
  • 22
  • Do you really want to tap on a particular UI element to switch to the other view controller or is just selecting the cell sufficient?. If selecting the cell is sufficient connect the segue to the table view cell (rather than the view controller), the `sender` parameter in `prepareForSegue` is the selected `UITableViewCell` instance. – vadian Nov 23 '15 at 13:34
  • 1
    Give your segue a 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) – harsha yarabarla Nov 23 '15 at 13:36
  • @harshayarabarla how would you exactly put that code into my label click: func handleTap(gestureRecognizer: UIGestureRecognizer) { let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc: AnyObject! = storyboard.instantiateViewControllerWithIdentifier("nextlevelnow") self.presentViewController(vc as! UIViewController, animated: true, completion: nil) – Frederic Nov 23 '15 at 13:43
  • 1
    @Frederic first give the segue an identifier in storyboard later replace self.presentViewController(vc as! UIViewController, animated: true, completion: nil) with self.performSegueWithIdentifier("identifier", sender: self) – harsha yarabarla Nov 23 '15 at 13:47
  • How is a button anything more than a label that reacts to user interaction? – nhgrif Nov 23 '15 at 13:56
  • Possible duplicate of [IOS7, Segue and storyboards - How to create without a button?](http://stackoverflow.com/questions/21205485/ios7-segue-and-storyboards-how-to-create-without-a-button) – nhgrif Nov 23 '15 at 13:56
  • @harshayarabarla it worked! Thanks a lot! For some reason I can't mark yours as the answer, maybe it's because nhgrif has marked my question as a duplicate? – Frederic Nov 23 '15 at 14:03

2 Answers2

2

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.

farhadf
  • 1,918
  • 3
  • 19
  • 27
1

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)

harsha yarabarla
  • 506
  • 4
  • 11