英文:
Why do I have to call my protocol functions two times to make it work?
问题
我目前正在构建一个简单的应用来学习协议 - 代理功能。然而,我的代码不起作用,即使在我编辑了我的第一个练习应用程序的方式后它也不起作用。在`FirstViewController`中有一个`UILabel`和一个`UIButton`。当用户点击`UIButton`时,一个分段将他们带到`SecondViewController`,在那里他们应该在`UITextField`中输入他们的名字。之后,他们可以按`SecondViewController`中的`UIButton`,输入的名字应该显示在`FirstViewController`中的`UILabel`中。
我无法弄清楚为什么我必须在`@IBAction backButtonPresses()`中调用协议函数两次。此外,我也无法弄清楚如何在`FirstViewController`中处理相同的协议函数。
以下是`FirstViewController.swift`文件的代码:
```swift
import UIKit
class FirstViewController: UIViewController, SecondViewControllerDelegate {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var button: UIButton!
var labelText = ""
let secondVC = SecondViewController()
func changeLabelText(name: String) {
labelText = secondVC.enteredName
}
@IBAction func buttonPressed(_ sender: UIButton) {
performSegue(withIdentifier: "goToSecondVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as? SecondViewController
destinationVC?.delegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
nameLabel.text = secondVC.enteredName
}
}
以下是SecondViewController
的代码:
import UIKit
protocol SecondViewControllerDelegate {
func changeLabelText(name: String)
}
class SecondViewController: UIViewController, SecondViewControllerDelegate {
var delegate: SecondViewControllerDelegate?
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var backButton: UIButton!
var enteredName: String = ""
func changeLabelText(name: String) {
enteredName = name
}
@IBAction func backButtonPressed(_ sender: UIButton) {
changeLabelText(name: nameTextField.text ?? "")
delegate?.changeLabelText(name: nameTextField.text ?? "")
print("das ist der name \(enteredName)")
dismiss(animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
英文:
I'm currently building a simple app to learn the protocol - delegate functionality. However my code doesn't work, even after I edited the way I did in my first practice app where it worked. In the FirstViewController
there is a UILabel
and a UIButton
. When the user taps on the UIButton
, a segue brings them to the SecondViewController
where they should enter their name into a UITextField
. After that, they can press the UIButton
in the SecondViewController
and the entered Name should be displayed in the UILabel
in the FirstViewController
.
I can't figure out why I have to call the protocol function two times in the @IBAction backButtonPresses()
. Also, I can't figure out how to handle the same protocol function in the FirstViewController
.
Here's the code for the FirstViewController.swift
file:
import UIKit
class FirstViewController: UIViewController, SecondViewControllerDelegate {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var button: UIButton!
var labelText = ""
let secondVC = SecondViewController()
func changeLabelText(name: String) {
labelText = secondVC.enteredName
}
@IBAction func buttonPressed(_ sender: UIButton) {
performSegue(withIdentifier: "goToSecondVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as? SecondViewController
destinationVC?.delegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
nameLabel.text = secondVC.enteredName
}
}
And here's the code of the secondViewController
:
import UIKit
protocol SecondViewControllerDelegate {
func changeLabelText(name: String)
}
class SecondViewController: UIViewController, SecondViewControllerDelegate {
var delegate: SecondViewControllerDelegate?
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var backButton: UIButton!
var enteredName: String = ""
func changeLabelText(name: String) {
enteredName = name
}
@IBAction func backButtonPressed(_ sender: UIButton) {
changeLabelText(name: nameTextField.text ?? "")
delegate?.changeLabelText(name: nameTextField.text ?? "")
print("das ist der name \(enteredName)")
dismiss(animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
答案1
得分: 2
在backButtonPressed
方法中,您没有两次调用任何委托方法。您调用了两个完全不同的changeLabelText
方法。一个在SecondViewController
中,另一个在委托上。
在SecondViewController
中对changeLabelText
的调用是相当不必要的。实际上,SecondViewController
中的enteredName
属性是不必要的。此外,SecondViewController
不应该实现自己的委托。
在delegate
上调用changeLabelText
就足够了。您只需要更新FirstViewController
中changeLabelText
的实现,以使用新值更新标签的文本。
func changeLabelText(name: String) {
nameLabel.text = name
}
不要在FirstViewController
中引用secondVC
属性。实际上,完全删除该属性。它不需要,并且引用了一个完全不同的SecondViewController
实例,而不是您通过segue实际呈现的那个。
我还建议将委托方法从changeLabelText
重命名为更一般的名称,例如enteredText
。SecondViewController
可以被任何其他视图控制器用于获取一些文本。它不仅仅是用于更改标签的。
以下是建议更改后的两个视图控制器:
class FirstViewController: UIViewController, SecondViewControllerDelegate {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var button: UIButton!
func enteredText(name: String) {
nameLabel.text = name
}
@IBAction func buttonPressed(_ sender: UIButton) {
performSegue(withIdentifier: "goToSecondVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as? SecondViewController
destinationVC?.delegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
protocol SecondViewControllerDelegate {
func enteredText(name: String)
}
class SecondViewController: UIViewController {
var delegate: SecondViewControllerDelegate?
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var backButton: UIButton!
@IBAction func backButtonPressed(_ sender: UIButton) {
let name = nameTextField.text ?? ""
delegate?.enteredText(name: name)
print("das ist der name \(name)")
dismiss(animated: true)
}
}
英文:
You do not call any delegate method twice in backButtonPressed
. You are calling two completely different changeLabelText
methods. One is in SecondViewController
, the other is on the delegate.
The call to changeLabelText
in the SecondViewController
is quite unnecessary. In fact, the enteredName
property in SecondViewController
is unnecessary. Also, SecondViewController
should not be implementing its own delegate.
The call to changeLabelText
on delegate
is all you need. You just need to update the implementation of changeLabelText
in FirstViewController
to update the label's text with the new value.
func changeLabelText(name: String) {
nameLabel.text = name
}
Do not reference the secondVC
property in FirstViewController
. In fact, remove that property completely. It's not needed and it's referencing a completely different instance of SecondViewController
than the one you actually present via segue.
I also suggest renaming the delegate method from changeLabelText
to something more general such as enteredText
. SecondViewController
can be used by any other view controller to obtain some text. It's not specific to changing a label.
Here's your two view controllers with suggested changes:
class FirstViewController: UIViewController, SecondViewControllerDelegate {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var button: UIButton!
func enteredText(name: String) {
nameLabel.text = name
}
@IBAction func buttonPressed(_ sender: UIButton) {
performSegue(withIdentifier: "goToSecondVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as? SecondViewController
destinationVC?.delegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
protocol SecondViewControllerDelegate {
func enteredText(name: String)
}
class SecondViewController: UIViewController {
var delegate: SecondViewControllerDelegate?
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var backButton: UIButton!
@IBAction func backButtonPressed(_ sender: UIButton) {
let name = nameTextField.text ?? ""
delegate?.changeLabelText(name: name)
print("das ist der name \(name)")
dismiss(animated: true)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论