gpt4 book ai didi

swift - 使用 UI 保存按钮更新文本字段(快速)

转载 作者:行者123 更新时间:2023-11-30 13:18:18 34 4
gpt4 key购买 nike

我正在尝试更新文本字段中的信息,因此当按下保存按钮时,它会更新字典中的信息并反射(reflect)在之前的 UITable 上。这是学生信息快速页面的代码。我似乎无法让它更新它保持不变的信息。

protocol saveProtocol
{
func updateStudentInfo(firstName: String?)
}


class StudentInfoViewController: UIViewController {

var studentRecord = Dictionary<String, String>()
var delegate: saveProtocol?

@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var emailAddress: UITextField!
@IBOutlet weak var studentMajor: UITextField!

@IBOutlet weak var numberOfCredits: UITextField!


override func viewDidLoad() {
super.viewDidLoad()
self.title = studentRecord["firstName"]! + " " + studentRecord["lastName"]!
// Do any additional setup after loading the view.
firstName.text = studentRecord["firstName"]
lastName.text = studentRecord["lastName"]
emailAddress.text = studentRecord["email"]
studentMajor.text = studentRecord["major"]
numberOfCredits.text = studentRecord["numberOfCredits"]
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func updateStudentInfo(sender: UIBarButtonItem) {


delegate?.updateStudentInfo(studentRecord["firstName"])
self.navigationController?.popViewControllerAnimated(true)

}

}

最佳答案

您应该在 updateStudentInfo(sender:) 方法中更新 studentRecords 字典。由于 Swift.String 是值类型,而不是引用类型,因此更改 firstName.text 不会更改 studentRecords 中的内容

@IBAction func updateStudentInfo(sender: UIBarButtonItem) {
self.studentRecord["firstName"] = firstName.text
self.studentRecord["lastName"] = lastName.text
// + all the other ones


delegate?.updateStudentInfo(studentRecord["firstName"])
self.navigationController?.popViewControllerAnimated(true)

}

关于swift - 使用 UI 保存按钮更新文本字段(快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38022512/

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