无法在视图控制器之间传递图像
Unable to pass image between view controllers
如问题所问。在我的第一个 VC 中,我设置了一个将图像传递给另一个 VC 的函数,如下所示。
@objc func imageTapped(tapGestureRecognizer1: UITapGestureRecognizer) {
let passimage = tapGestureRecognizer1.view as! UIImageView
let alertController = UIAlertController(title: "...", message: "...", preferredStyle: UIAlertController.Style.alert)
let ok = UIAlertAction(title: "...", style: UIAlertAction.Style.default, handler: {(action) -> Void in
self.performSegue(withIdentifier: "Shop", sender: self)
print("image tapped")
let sb = self.storyboard?.instantiateViewController(withIdentifier: "shopVC") as! shopVC
sb.loadimage = passimage.image
self.present(sb, animated: true, completion: nil)
})
alertController.addAction(ok)
alertController.addAction(UIAlertAction(title: "取消(留在此版面)", style: UIAlertAction.Style.cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
在另一个 VC 上,我设置了一个图像视图,它会读取此处显示的图像。
class shopVC: UIViewController {
@IBOutlet weak var clothimage: UIImageView!
var loadimage: UIImage?
override func viewDidLoad() {
clothimage.image = loadimage
super.viewDidLoad()
}
我知道“passimage.image”是我想要的,因为它显示在调试中 window - 我似乎无法将它放在第二个 VC 中。
需要先调用super.viewDidLoad()再设置clothimage
更改 ViewController 您同时使用两个选项,单独使用时,每个:
// optionOne
self.performSegue(withIdentifier: "Shop", sender: self)
print("image tapped")
// optionTwo
let sb = self.storyboard?.instantiateViewController(withIdentifier: "shopVC") as! shopVC
sb.loadimage = passimage.image
self.present(sb, animated: true, completion: nil)
如果使用选项一,您必须从 firstVC 覆盖以下函数并直接设置图像:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? shopVC {
vc.image = yourImage
}
}
如果您将 present 用于 show ShopVC,则无需更改代码,只需注释 performSegue 函数即可。
这意味着只有以下代码:
let sb = self.storyboard?.instantiateViewController(withIdentifier: "shopVC") as! shopVC
sb.loadimage = passimage.image
self.present(sb, animated: true, completion: nil)
如问题所问。在我的第一个 VC 中,我设置了一个将图像传递给另一个 VC 的函数,如下所示。
@objc func imageTapped(tapGestureRecognizer1: UITapGestureRecognizer) {
let passimage = tapGestureRecognizer1.view as! UIImageView
let alertController = UIAlertController(title: "...", message: "...", preferredStyle: UIAlertController.Style.alert)
let ok = UIAlertAction(title: "...", style: UIAlertAction.Style.default, handler: {(action) -> Void in
self.performSegue(withIdentifier: "Shop", sender: self)
print("image tapped")
let sb = self.storyboard?.instantiateViewController(withIdentifier: "shopVC") as! shopVC
sb.loadimage = passimage.image
self.present(sb, animated: true, completion: nil)
})
alertController.addAction(ok)
alertController.addAction(UIAlertAction(title: "取消(留在此版面)", style: UIAlertAction.Style.cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
在另一个 VC 上,我设置了一个图像视图,它会读取此处显示的图像。
class shopVC: UIViewController {
@IBOutlet weak var clothimage: UIImageView!
var loadimage: UIImage?
override func viewDidLoad() {
clothimage.image = loadimage
super.viewDidLoad()
}
我知道“passimage.image”是我想要的,因为它显示在调试中 window - 我似乎无法将它放在第二个 VC 中。
需要先调用super.viewDidLoad()再设置clothimage
更改 ViewController 您同时使用两个选项,单独使用时,每个:
// optionOne
self.performSegue(withIdentifier: "Shop", sender: self)
print("image tapped")
// optionTwo
let sb = self.storyboard?.instantiateViewController(withIdentifier: "shopVC") as! shopVC
sb.loadimage = passimage.image
self.present(sb, animated: true, completion: nil)
如果使用选项一,您必须从 firstVC 覆盖以下函数并直接设置图像:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? shopVC {
vc.image = yourImage
}
}
如果您将 present 用于 show ShopVC,则无需更改代码,只需注释 performSegue 函数即可。 这意味着只有以下代码:
let sb = self.storyboard?.instantiateViewController(withIdentifier: "shopVC") as! shopVC
sb.loadimage = passimage.image
self.present(sb, animated: true, completion: nil)