swift - 从 ViewController 传递数据

标签 swift swift3 textview segue

我已经设置了一个到另一个 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/

相关文章:

ios - 如何将 UIButton 连接到选项卡栏 Controller 中的特定选项卡?

ios - Google map ,使用 DispatchQueue 加载标记

ios - 带有 Action 的 UITableViewCell 按钮

android - 向 textView 添加多个值 - Android

android - 如何在 Android 上的 TextVIew 上实现 5 秒内从 0 到 600 的数字动画

android - android studio 中不是全宽工具栏?

swift - 两个日期之间没有毫秒差异

ios - 将 indexpath 标记传递给 tableview 中原型(prototype)单元格内的按钮

ios - 禁用长按

ios - SceneKit如何绘制上万条线?