简体   繁体   中英

Why do I have to call my protocol functions two times to make it work?

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()
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM