我使用autolayout并且在我的生活中无法使用先前的视图控制器集合视图单元格的图像设置前进的视图控制器图像视图。我正在使用
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "openDetailView", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "openDetailView" {
let cell = sender as? ImageCollectionViewCell
let detailVC = segue.destination as! DetailedImageViewController
detailVC.imageToPresent = cell?.imageView.image
}
}
这是我已设置接收图像的课程
class DetailedImageViewController : UIViewController{
var imageToPresent: UIImage!
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = imageToPresent
}
}
segue按预期执行,但图像不会显示在detailedImageViewController中。提前谢谢。
答案 0 :(得分:0)
<击> 您需要将imageToPresent设置为UIImageview以显示图像。
class DetailedImageViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
var imageToPresent : UIImage!
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = imageToPresent
}
...
击> <击> 撞击>
答案 1 :(得分:0)
在这种情况下避免可选的展开不是最佳做法。尝试使用此代码并检查错误是否发生。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "openDetailView" {
guard let cell = sender as? ImageCollectionViewCell else {
assertionFailure("Failed to unwrap sender. Try to set a breakpoint here and check what sender is")
return
}
let detailVC = segue.destination as! DetailedImageViewController
guard let cellImage = cell.imageView.image else {
assertionFailure("The cell has no image in image view")
return
}
detailVC.imageToPresent = cellImage
}
}