准备继续查看控制器时出现致命错误 [英] fatal error when prepared to segue to view controller

查看:19
本文介绍了准备继续查看控制器时出现致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在 destViewController.titleLabel.text = "Testing Segue" 处解开可选值时遇到意外发现 nil 的致命错误.

I'm currently getting a fatal error unexpectedly found nil while unwrapping an optional value at destViewController.titleLabel.text = "Testing Segue".

我如何修复它,因为它在转到 SecondViewController 时出现错误?如何避免在 titleLabel.text 处变为 nil?

How do I fixed it since it getting me a error when it segue to SecondViewController? How do I avoid getting nil at titleLabel.text?

class ViewController: UIViewController {

        @IBAction func action(sender: AnyObject) {

            let alertController: UIAlertController = UIAlertController (title: "Next Page", message: "", preferredStyle: .Alert)

            let yesAction = UIAlertAction (title: "YES", style: .Default ) { action -> Void in self.performSegueWithIdentifier("test", sender: self)
            }

            alertController.addAction (yesAction)

            self.presentViewController(alertController, animated: true, completion: nil)

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

            if (segue.identifier == "test") {

               let destViewController : SecondViewController = segue.destinationViewController as SecondViewController

                destViewController.titleLabel.text = "Testing Segue"

            }
        }
    }

推荐答案

首先像这样更新 ViewController 中的代码:

First Of all Update your code in your ViewController like this:

class ViewController: UIViewController {

@IBAction func action(sender: AnyObject) {

    let alertController: UIAlertController = UIAlertController (title: "Next Page", message: "", preferredStyle: .Alert)

    let yesAction = UIAlertAction (title: "YES", style: .Default ) { action -> Void in self.performSegueWithIdentifier("test", sender: self)
    }

    alertController.addAction (yesAction)

    self.presentViewController(alertController, animated: true, completion: nil)
}

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

    if (segue.identifier == "test") {

        let destViewController : SecondViewController = segue.destinationViewController as SecondViewController

        destViewController.textlbl = "Testing Segue"

        }

    }
}

在那之后,我认为你不能用你的方式为 textLabel 赋值,所以在你的 SecondViewController 中以这种方式为该标签赋值:

After that I think you can not assingn value to textLabel with your way so assign value to that label this way in your SecondViewController:

import UIKit

class SecondViewController: UIViewController {

@IBOutlet weak var titleLabel: UILabel!
var textlbl = String()
override func viewDidLoad() {
    super.viewDidLoad()

    self.titleLabel.text = textlbl
    }
}

还有 HERE 我创建了一个示例项目供您参考.

And HERE I cerated a sample project for you for more reference.

这篇关于准备继续查看控制器时出现致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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