(Swift)PrepareForSegue:致命错误:在展开Optional值时意外发现nil [英] (Swift) PrepareForSegue: fatal error: unexpectedly found nil while unwrapping an Optional value

查看:108
本文介绍了(Swift)PrepareForSegue:致命错误:在展开Optional值时意外发现nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DetailViewController:

  @IBOutlet var selectedBundesland:UILabel! 

TableViewController:

 覆盖func prepareForSegue(segue:UIStoryboardSegue!,sender:AnyObject!){


if(segue.identifier ==BackToCalculator){
var vc :FirstViewController = segue.destinationViewController as FirstViewController
vc.selectedBundesland.text =Test
}

IBOutlet已连接!



错误:致命错误:在解包可选值时意外发现nil



<我读了很多关于Optionals的页面,但我不知道我的问题的答案。



您是否需要有关我的项目的更多信息?

解决方案

您不能直接写入中的 UILabel prepareForSegue 因为视图控制器尚未完全初始化。您需要创建另一个字符串属性来保存该值并将其放入相应函数的标签中 - 例如 viewWillAppear


DetailViewController:

    @IBOutlet var selectedBundesland: UILabel!

TableViewController:

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {


    if (segue.identifier == "BackToCalculator") {
        var vc:FirstViewController = segue.destinationViewController as FirstViewController
            vc.selectedBundesland.text = "Test"
    }

IBOutlet is connected!

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

I read multiple pages about Optionals but i didn't know the answer to my problem.

Do you need more information about my project?

解决方案

You cannot write directly to the UILabel in prepareForSegue because the view controller is not fully initialised yet. You need to create another string property to hold the value and put it into the label in the appropriate function - such as viewWillAppear.

这篇关于(Swift)PrepareForSegue:致命错误:在展开Optional值时意外发现nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆