prepareForSegue function not working

I am sure I have two view controllers, that have the appropriate files as their classes. (ViewController1.swift and ViewController2.swift) I am trying to pass data from one of the view controllers to the other one with the following code; but I get the error "UIView Controller is not convertible to ViewController2". What should I do? I tried to change as to as! and var to let; neither worked.

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!){

        if (segue.identifier == "btnSubmitSegue") {
        var svc = segue.destinationViewController as ViewController2;
        svc.DataPassed = textField.text

    }
}

the code for ViewController2 import UIKit

 class ViewController2: UIViewController {

var toPass: String = ""
 @IBOutlet weak var labeld: UILabel!



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

      self.labeld.text = "passed: \(self.toPass)"

}




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

the code for ViewController1

  import UIKit

  class ViewController1: UIViewController {

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.
}



@IBOutlet weak var textField: UITextField!

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "TheSegue" {
        let vc = segue.destinationViewController as! ViewController2
        vc.toPass = self.textField.text!
    }
  }


}

Answers


The following example works fine for me. Code for the first view controller:

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "TheSegue" {
            let vc = segue.destinationViewController as! ViewController2
            vc.passedText = self.textField.text!
        }
    }
}

And in the second view controller:

    class ViewController2: UIViewController {
        var passedText: String = ""
        @IBOutlet weak var label: UILabel!

        override func viewDidLoad() {
            super.viewDidLoad()
            self.label.text = "passed: \(self.passedText)"
        }
    }

Don't forget to name the segue in the Storyboard:

Your destination view controller is referring to the wrong controller. You should have this in your code, not as! ViewController1:

let vc = segue.destinationViewController as! ViewController2

Also need to fix your IBOutlet name, the vc.toPass = self.textField.text! should be:

vc.toPass = self.labeld.text!

Make sure the custom class is set for the second view controller:


It may just be that ViewController2 is not declared as a subclass as UIViewController. At the top of the ViewController2 file you would declare it as follows:

    class ViewController2: UIViewController {

    \\ ...

}

Have you set the name in the Storyboard ? Under custom class, Class needs to point to the view controllers associated class.

this class is not key value coding-compliant for the key labelPassedData.' But I do not even have a variable like labelPassedData

Click you view controller and press right hand arrow button, it shows all connections.

I guess you copy and pasted the view controllers and hence you get this problem.


Need Your Help

django-ckeditor to Django admin

django django-admin django-ckeditor

I've installed django-ckeditor on django 1.5 as instructed in the docs.

Why doesn't GCJ find the classes from my imported packages?

java linker gcj

I want to compile a small Java application to a Windows executable.