gpt4 book ai didi

ios - 无法访问通过委托(delegate)传递的数据

转载 作者:行者123 更新时间:2023-11-30 12:03:08 24 4
gpt4 key购买 nike

我试图理解委托(delegate),但我不明白为什么它说我使用了无法解析的标识符“数据”。

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

}

问题在于下面这段代码对于我的接收VC,我无法访问应由委托(delegate)传递的数据变量。

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

}

最佳答案

更新您的代码:

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

关于ios - 无法访问通过委托(delegate)传递的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46982128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com