如何在Swift(iOS)中在View Controllers之间传递数据?
大家好!我是iOS开发新手,使用Swift,一直搞不懂如何在屏幕之间传递数据。我有两个View Controller:`FirstViewController`和`SecondViewController`。第一个有一个文本字段(UITextField),我想在点击按钮后,该字段中的文本显示在第二个View Controller的UILabel中。
我尝试通过segue传递数据,但没成功。这是我的代码:
```swift
// FirstViewController.swift
import UIKit
class FirstViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSecondVC" {
let secondVC = segue.destination as! SecondViewController
secondVC.label.text = textField.text // 这里出错
}
}
}
// SecondViewController.swift
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var label: