0

I have been struggling to pass data between view controllers using segues. I have watched many youtube video tutorials and searched around on forums but I can't figure out why it won't work. I don't get any error messages but when I click the 'Button' the simulator crashes. I would love some help! Thanks!

Here is the code :

ViewController1 :

    import UIKit

class ViewController: UIViewController {

    @IBOutlet var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let destViewController : ViewController2 = segue.destinationViewController as! ViewController2

        destViewController.labelText = textField.text!

    }

}

ViewController2 :

import Foundation
import UIKit

class ViewController2 : UIViewController {

    @IBOutlet var Label: UILabel!

    var labelText : String = ""

    override func viewDidLoad() {

        Label.text = labelText

    }

}

StoryBoard Image :

storyboard image

Alfie
  • 1
  • 5
  • Which line it is crashing? – Santosh Sep 20 '16 at 16:07
  • @Alife you missed that segue.identifier = " some identifier" – Shanmugasundharam Sep 20 '16 at 16:26
  • on your viewController2 try to change this line var labelText : String = "" to this var labelText : String! To prevent simulator from crashing try changing this " destViewController.labelText = textField.text!" to this " destViewController.labelText = textField.text ?? "text field is empty thats why nothing happens" " – Noel Carcases Sep 20 '16 at 17:46
  • @Shanmugasundharamselvadurai where to i insert the segue identifier? – Alfie Sep 21 '16 at 21:48
  • @Santosh I get the error 'Thead 1: Signal SIGABRT' in my appDelegate.swift file but i checked all of the connections. – Alfie Sep 21 '16 at 21:50
  • @NoelCarcases I tried both of the things that you suggested but neither made any change. When I tried your second suggestion, it gave me the error 'Cannot use optional chaining on non-optional value of type String'. – Alfie Sep 21 '16 at 21:54
  • What the crash logs says in the console ? Are you able to see that logs? When it crashes just click continue, you would be able to see the logs – Santosh Sep 21 '16 at 22:04
  • @NoelCarcases I couldn't use the will appear method. It said i had to use "," as a separator so I did. It then gave me another error but I don't know what the fix is or what the error message is. Also, I cleaned my project but it still crashes. – Alfie Sep 22 '16 at 17:44
  • @Santosh **The crash logs :** _2016-09-22 18:47:14.192 ViewControllerDataPassingTest[51357:3697272] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Push segues can only be used when the source controller is managed by an instance of UINavigationController.' *** First throw call stack:_ – Alfie Sep 22 '16 at 17:48
  • On storyboard click on the segue and change the type to detail or embebed, the error indicate that you are not using a navigation controller so push segue will not work for you – Noel Carcases Sep 23 '16 at 07:10
  • Later I will power on the computer to check but I think modal is fine. I will argument on that later on an answer – Noel Carcases Sep 23 '16 at 18:21
  • @NoelCarcases Thank you so much! I changed it from Push to Modal and now it works fine! – Alfie Sep 23 '16 at 18:30
  • I have posted an answer with a link to another question, please note that push and modal segues are deprecated. – Noel Carcases Sep 23 '16 at 19:03

3 Answers3

0

Make sure to enter some text in your label textField before tapping the button since you are passing that label's text to next scene by using force unwrap.

  destViewController.labelText = textField.text! //here compiler expects that your textField has the value
Santosh
  • 2,900
  • 1
  • 16
  • 16
0

I made a sample project using your same code and didn't get an error. There's nothing wrong with your code.

Try this: Go to your storyboard and check outlets inspector for each view controller and make sure you don't have an outlet to nowhere.

nighttalker
  • 896
  • 6
  • 13
  • I checked and didn't find anything. I do get a SIGABRT error though. – Alfie Sep 21 '16 at 21:58
  • That's the error you normally get with unconnected outlets. All your outlets should have a filled in circle. You should have 2 outlets on ViewController (textField and view) and 2 outlets on ViewController 2 (Label and view). – nighttalker Sep 21 '16 at 22:32
0

As stated on the comment the problem was that you were using a push segue on a UIViewController.

Please note that push and modal segues are deprecated... Please take a look at this post entry and let me know if you have any doubts

What's the difference between all the Selection Segues?

Community
  • 1
  • 1
Noel Carcases
  • 711
  • 1
  • 7
  • 27