我正在尝试理解委托,但我不明白为什么它说我使用了未解析的标识符'数据'。
//This is my sendingVC
import UIKit
protocol TextFieldDelegate {
func userEnteredText(text: String)
}
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
var delegate: TextFieldDelegate? = nil
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sendButton(_ sender: AnyObject) {
if delegate != nil {
if textField.text != nil {
let data = textField.text
delegate?.userEnteredText(text: data!)
}
}
}
}
问题在于以下代码对于我的receiveVC我无法访问应该由委托传递的数据变量。
//This is my receivingVC
import UIKit
class SecondViewController: UIViewController, TextFieldDelegate {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
func userEnteredText(text: String) {
label.text = data // Use of unresolved identifier 'data'
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "sendSegue" {
let destVC: ViewController = segue.destination as! ViewController
destVC.delegate = self
}
}
}
答案 0 :(得分:0)
更新您的代码:
func userEnteredText(text: String) {
label.text = text
}