简体   繁体   English

PrepareForSegue Swift(更改UIImage和UILabel)

[英]PrepareForSegue Swift (changing UIImage and UILabel)

So I'm writing the prepareForSegue for my app but having issue setting up the code for changing the UIImage and UILabel for the destination ViewController . 所以我正在为我的应用程序编写prepareForSegue ,但是在设置用于更改目标ViewControllerUIImageUILabel的代码时遇到了问题。

What would be the code to change the images and UILabel when segue to the next ViewController ? 当选择到下一个ViewController时,更改图像和UILabel的代码是什么?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "toTest") {            
        let destViewController : data3ViewController = segue.destinationViewController as data3ViewController    
    }
}

I you have in your data3ViewController an UILabel and an UIImage , let says named myLabel and myImage respectively , then you should do the following : 我在data3ViewController一个UILabelUIImage ,分别说名为myLabelmyImage ,那么您应该执行以下操作:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   if (segue.identifier == "toTest") {            
      var destViewController : data3ViewController = segue.destinationViewController as data3ViewController   
      let _ =  destViewController.view
      destViewController.myLabel.text = "Something"
      destViewController.myImage = UIImage(named: "photo")
   }
}

The call above to destViewController.view ensure that the @IBOutlet 's are initialized before set it values. 上面对destViewController.view的调用确保在设置@IBOutlet的值之前对其进行初始化。

I hope this help you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM