准备继续查看控制器时出现致命错误

fatal error when prepared to segue to view controller

我目前在 destViewController.titleLabel.text = "Testing Segue".

解包可选值 时遇到 意外发现 nil 的致命错误

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

处得到 nil
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 中的代码:

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:

中用这种方式给那个标签赋值
import UIKit

class SecondViewController: UIViewController {

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

    self.titleLabel.text = textlbl
    }
}

并且HERE我创建了一个示例项目供您参考。