OStack程序员社区-中国程序员成长平台

标题: ios - 无法通过当前模态序列将图像传递给另一个 UIViewController [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 16:47
标题: ios - 无法通过当前模态序列将图像传递给另一个 UIViewController

我试图将图像传递给目标 UIView 的另一个 imageView,该目标 UIView 连接到源 UIView 超过当前的模态 segue。

我试图通过 segue 传递数据,并通过 Outlet 在源 UIView 中设置图像(即 NSData - 核心数据),但我收到错误 image description 在控制台中 -

"fatal error: unexpectedly found nil while unwrapping an Optional value"

  //Code of destination UIView
   import UIKit
   import CoreData
   class ReviewViewController: UIViewController {

   @IBOutlet var backgroundImageView:UIImageView!
   var subject:Semester!       

    override func viewDidLoad() {
    super.viewDidLoad()
    backgroundImageView?.image = UIImage(data: subject.image!)
    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = view.bounds
    backgroundImageView.addSubview(blurEffectView)

   // Code of Source UIView
  import UIKit
  import CoreData

  class SubjectDetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  @IBOutlet var facultyImageView:UIImageView!
  var subject:Semester!

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "destinationidentifier"
    {
            let destinationController = segue.destinationViewController as! ReviewViewController
            destinationController.backgroundImageView?.image = self.facultyImageView.image
     }
}



Best Answer-推荐答案


您的问题是 backgroundImageView 对象为零,因为 destinationController 当前不在 window 层次结构中。

所以像这样改变你的 prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "destinationidentifier"
    {
        let destinationController = segue.destinationViewController as! ReviewViewController
        destinationController.image = self.facultyImageView.image
    }
} 

现在在 ReviewViewController 中创建一个图像对象,如下所示,并在 viewDidLoad

中添加一行
var image: UIImage!

override func viewDidLoad() {
    super.viewDidLoad()
    self.backgroundImageView?.image = self.image
}

希望对你有帮助

关于ios - 无法通过当前模态序列将图像传递给另一个 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37936430/






欢迎光临 OStack程序员社区-中国程序员成长平台 (http://ostack.cn/) Powered by Discuz! X3.4