【问题标题】:Swift Can't initializing UIImageView and causes fatal error: unexpectedly found nilSwift 无法初始化 UIImageView 并导致致命错误:意外发现 nil
【发布时间】:2017-04-04 18:08:28
【问题描述】:

我使用的是 xcode 8.2.1

致命错误:在展开可选值时意外发现 nil

这是代码,

class ShowMediaViewController: UIViewController {
var image: UIImage?
var titreText: String!
@IBOutlet var imageView: UIImageView!
//i tried @IBOutlet weak var imageView: UIImageView! but didn't work
@IBOutlet weak var titre: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    if image != nil {

        //crashes here, because imageView is nil
        imageView.image = image
    } else {
        print("image not found")
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.
}

}

代码似乎在这一行崩溃

imageView.image = image

我认为是因为 imageView 是 nil?,因为我试过了

print(image)

结果很好,然后 print(imageView),它会导致致命错误 但我确实初始化了它

@IBOutlet var imageView: UIImageView!

也许我的故事板有问题?

任何帮助将不胜感激

**

更新 1 连接检查器

pic 1

pic 2 **

【问题讨论】:

  • 你能显示那个 imageView 的连接检查器的图片吗?
  • 在 Storyboard 中,右键单击ImageView 并三次检查 1) 与您的插座的连接是否正确,以及 2) 没有其他连接(在重命名插座等后可能仍然存在)。

标签: swift xcode uiimageview xcode8 xcode8.2


【解决方案1】:

我遇到了同样的问题。我尝试了以下步骤。然后就可以了。

试试这些,

  1. 检查 imageView 是否正确绑定到 storyBoard。
  2. 清理并构建项目。

如果不行

  1. 关闭并重新启动 XCode
  2. 从 storyBoard 中移除 imageView 再次添加 imageView 并再次绑定。

【讨论】:

    【解决方案2】:

    您可能缺少从视图到插座的连接。检查这些:-

    1) 单击代码中插座旁边的一个点会显示与情节提要的连接 2) 图片的 outlets 窗口显示了与代码的连接

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多