gpt4 book ai didi

xcode - 准备继续查看 Controller 时出现 fatal error

转载 作者:可可西里 更新时间:2023-11-01 01:42:18 25 4
gpt4 key购买 nike

我目前在 destViewController.titleLabel.text = "Testing Segue"处收到一个 fatal error ,意外发现 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我创建了一个示例项目供您引用。

关于xcode - 准备继续查看 Controller 时出现 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28640685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com