我试图将一个uiimage传递给我的secondViewController,但是我的编码一直出错。如何将uiimage分割到第二个viewcontroller?
视图控制器.swift
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "test") {
let destViewController : SecondViewController = segue.destinationViewController as SecondViewController
destViewController.mainpictures = "background"
}
第二视图控制器
class SecondViewController: UIViewController {
var mainpictures = String ()
@IBOutlet var mainImage: UIImageView!
override func viewDidLoad() {
self.mainImage.text = mainpictures
super.viewDidLoad()
}
最佳答案
编写此代码
if let image = UIImage(named: mainpictures) {
self.mainImage.image = image
}
而不是这个
self.mainImage.text = mainpictures