无法访问通过委托传递的数据

时间:2017-10-27 19:25:15

标签: ios swift delegates

我正在尝试理解委托,但我不明白为什么它说我使用了未解析的标识符'数据'。

//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
    }
}

}

1 个答案:

答案 0 :(得分:0)

更新您的代码:

func userEnteredText(text: String) {
    label.text = text
}