EQOxx123 EQOxx123 - 6 months ago 30
iOS Question

Error With prepareForSegue in Swift

I am trying to have it so that I can fill in 8 UITextFields in my ViewController view and then when I tap my button 'nextView' is called on. If all requirements are met (there is a UIImage for that string) I use self.presentViewController(...) to navigate to my next view. However, in that next view, my 8 images are not updated(image1 - image8). How can I solve this. Thank you. Below is my code and any advice would be great!

class ViewController : UIVewController {

// field1 - field8 are defined here as UITextFields

@IBAction func nextView(sender: UIButton) {

if (my requirements are met) {

let storyboard = UIStoryboard(name: "Main", bundle: nil)

let nextViewController : UIViewController = storyboard.instantiateViewControllerWithIdentifier("View Two") as UIViewController

self.presentViewController(nextViewController, animated: true, completion: nil)

} else {

// nothing happens

}


}

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

let Dest : ViewTwo = segue.destinationViewController as! ViewTwo

Dest.c1 = field1.text! // c1 - c8 are strings in ViewTwo
Dest.c2 = field2.text! // field1 - field8 are UITextFields in ViewController
Dest.c3 = field3.text!
Dest.c4 = field4.text!
Dest.c5 = field5.text!
Dest.c6 = field6.text!
Dest.c7 = field7.text!
Dest.c8 = field8.text!

}

}

class ViewTwo : UIViewController {

var c1 = String()
var c2 = String()
var c3 = String()
var c4 = String()
var c5 = String()
var c6 = String()
var c7 = String()
var c1 = String()
var c8 = String()

override func viewDidLoad() {

image1.image = UIImage(named: "\(c1)") // image1 - image8 are UIImageView s in ViewTwo
image2.image = UIImage(named: "\(c2)")
image3.image = UIImage(named: "\(c3)")
image4.image = UIImage(named: "\(c4)")
image5.image = UIImage(named: "\(c5)")
image6.image = UIImage(named: "\(c6)")
image7.image = UIImage(named: "\(c7)")
image8.image = UIImage(named: "\(c8)")

image1.frame = CGRectMake(20, 219, 80, 80) // these are the image’s dimensions
image3.frame = CGRectMake(20, 302, 80, 80)
image5.frame = CGRectMake(20, 385, 80, 80)
image7.frame = CGRectMake(20, 468, 80, 80)
image2.frame = CGRectMake(220, 219, 80, 80)
image4.frame = CGRectMake(220, 302, 80, 80)
image6.frame = CGRectMake(220, 385, 80, 80)
image8.frame = CGRectMake(220, 468, 80, 80)

self.view.addSubview(image1) // here I add the images to the view
self.view.addSubview(image2)
self.view.addSubview(image3)
self.view.addSubview(image4)
self.view.addSubview(image5)
self.view.addSubview(image6)
self.view.addSubview(image7)
self.view.addSubview(image8)


}

}

Answer

Connect the two ViewControllers with segue, not button to ViewController two then do the following:

 @IBAction func nextView(sender: UIButton) {
         if (my requirements are met) {
              performSegueWithIdentifier("yourIdentifier", sender: nil)
         } else {
               // nothing happens
         }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "yourIdentifier" {
        let viewTwo = segue.destinationViewController as! ViewTwo

        viewTwo.c1 = field1.text! // c1 - c8 are strings in ViewTwo
        viewTwo.c2 = field2.text! // field1 - field8 are UITextFields in ViewController
        viewTwo.c3 = field3.text!
        viewTwo.c4 = field4.text!
        viewTwo.c5 = field5.text!
        viewTwo.c6 = field6.text!
        viewTwo.c7 = field7.text!
        viewTwo.c8 = field8.text!
}

    }

}