我已经设置了一个到另一个 Controller 的segue,并且我正在尝试将一些文本从textView传递到新 Controller 。当我运行程序时,我不断收到错误:“在展开可选值时意外发现 nil”
我知道我传递的值不是零...请参阅下面的 segue 代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "getReflection" {
if let destinationVC = segue.destination as? ReflectionViewController {
destinationVC.reflectionTextBox.text = self.reflectionTextBox.text
//Crashes on the above expression???
destinationVC.delegate = self
}
}
}
最佳答案
您的目标 ViewController 的 View 在准备时尚未实例化,因此 IBOutlet 绑定(bind)尚未发生。
在这种情况下,如果destinationVC.reflectionTextBox
定义为@IBOutlet weak var ReflectionTextBox: UITextView!
,则当!
可选的展开尝试返回对尚未绑定(bind)的 reflectionTextBox
的引用。 prepareSegue
应用于将数据传递到目标 View Controller ,然后目标 View Controller 可以在其 viewDidLoad
函数中查找这些参数,以便在任何 UI 组件中使用它们。
关于swift - 从 ViewController 传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40273042/