按下ViewController时无法传输图像

时间:2019-06-05 06:18:11

标签: swift

我有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,同时隐式展开一个可选值

3 个答案:

答案 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)

在您的第一个视图控制器中,selectedImageselectedTexttableView didSelectRowAt方法中将为零。 IBOutlet仅在您按下第二个视图控制器后才会启动。因此,在第二个视图控制器中创建一个UIImageString变量,并将值发送到这些变量。在第二个视图控制器的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)
}