【发布时间】:2018-12-31 07:40:39
【问题描述】:
我正在尝试在显示视图控制器时更改其 ImageView 图像。视图控制器显示在我的 AppDelegate 文件中。但是,每当触发逻辑时,应用程序就会崩溃 (lldb) 并且错误显示为:“线程 1:在展开可选值时意外发现 nil。”我可以使用它们的默认配置推送 ViewController,但是当从 AppDelegate 呈现它们时我无法编辑它们的变量。我在 AppDelegate 中的代码如下:
//the viewController to present
let viewController = storyboard.instantiateViewController(withIdentifier: "myViewController") as! myViewController
viewController.imageView.image = UIImage(named: "myImage.png")
// navigationController is my instantiated navigation controller in my storyboard
navigationController.pushViewController(viewController, animated: true)
我还制作了自己的 pushViewController 扩展来容纳完成处理程序:
navigationController.pushViewController(viewController, animated: true, completion: {
viewController.imageView.image = UIImage(named: "myImage.png")
})
不幸的是,这仍然不起作用。当我在其他不是 AppDelegate 的文件中执行此操作时,它工作正常。出于某种原因,我无法让它在 AppDelegate 中工作。任何帮助将不胜感激!
【问题讨论】:
-
一个非常常见的错误。在视图控制器被实例化的那一刻,出口还没有连接。使用属性传递图像并将图像分配给
viewDidLoad的myViewController中的图像视图。并且根据命名约定,类名以大写字母开头。