在Swift中使用prepareForSegue时出错

时间:2016-05-24 21:39:56

标签: ios swift

我想要它,以便我可以在我的ViewController视图中填写8个UITextFields,然后当我点击我的按钮' nextView'被召唤。如果满足所有要求(该字符串有UIImage),我使用self.presentViewController(...)导航到我的下一个视图。但是,在下一个视图中,我的8个图像未更新(image1 - image8)。我该怎么解决这个问题。谢谢。以下是我的代码,任何建议都会很棒!

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)


    }

}

1 个答案:

答案 0 :(得分:1)

将两个ViewControllers与segue连接,而不是按钮连接到ViewController两个,然后执行以下操作:

 @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!
}

    }

}