我有tableView,其中有imageView和label,当我选择单元格时,我想推送另一个viewController并在其中显示我选择的单元格imageView.image和label.text,但是有错误(意外地发现nil在图像行中隐式展开一个Optional值)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "PushedViewController") as? PushedViewController
vc?.pushedImage.image = UIImage(named: imagesArray[indexPath.row])
vc?.pushedLabel.text = imagesArray[indexPath.row]
self.navigationController?.pushViewController(vc!, animated: true)
}
意外发现nil,同时隐式展开一个可选值
答案 0 :(得分:0)
您发布的内容不多,但崩溃的行似乎是
self.navigationController?.pushViewController(vc!, animated: true)
您确定此storyboard?.instantiateViewController(withIdentifier: "PushedViewController") as? PushedViewController
不是nil
吗?
也许PushedViewController
不在同一个情节提要中。无论如何,只要将其放在guard let
中即可展开可选
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "PushedViewController") as? PushedViewController else { //fallback goes here return }
vc.pushedImage.image = UIImage(named: imagesArray[indexPath.row])
vc.pushedLabel.text = imagesArray[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
答案 1 :(得分:0)
在您的第一个视图控制器中,selectedImage
和selectedText
在tableView didSelectRowAt
方法中将为零。 IBOutlet
仅在您按下第二个视图控制器后才会启动。因此,在第二个视图控制器中创建一个UIImage
和String
变量,并将值发送到这些变量。在第二个视图控制器的viewDidLoad
方法中,将传递的值分配给IBOutlet
。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "PushedViewController") as? PushedViewController
//vc?.pushedImage vc?.pushedLabel are nil
vc?.selectedImage = UIImage(named: imagesArray[indexPath.row])
vc?.selectedText = imagesArray[indexPath.row]
self.navigationController?.pushViewController(vc!, animated: true)
}
PushedViewController
class PushedViewController: UIViewController {
@IBOutlet weak var pushedLabel: UILabel!
@IBOutlet weak var pushedImage: UIImageView!
var selectedImage: UIImage?
var selectedText: String?
override func viewDidLoad() {
super.viewDidLoad()
//vc?.pushedImage vc?.pushedLabel are not nil
pushedImage.image = selectedImage
pushedLabel.text = selectedText
}
}
答案 2 :(得分:0)
UI组件尚未初始化,因此您无法在初始化控制器后立即访问它们。
我建议您在PushedViewController
中有一个局部变量来存储图像和文本,然后在viewDidLoad
中分配它们。
赞:
class PushedViewController: UIViewController {
//..
var image:UIImage?
var text:String?
//..
override viewDidLoad() {
super.viewDidLoad()
//..
if let image = self.image {
self.imageView.image = image
}
if let text = self.text {
self.label.text = text
}
//..
}
//..
}
然后从推动控制器的位置设置这些属性:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "PushedViewController") as? PushedViewController else { return }
// Assign the properties here
vc.image = UIImage(named: imagesArray[indexPath.row])
vc.text = textsArray[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}