iosswiftclosurespass-data

How to send back data using Closures in Swift iOS?


I'm following this tutorial to send data back using Closures. https://betterprogramming.pub/5-ways-to-pass-data-between-view-controllers-18acb467f5ec

in this tutorial point no 4 that is "Closures". I have two VC's one for selecting pet (FormsVC) and one for displaying selected pet (ProfileVC).

below is a code for ProfileVC:

// ProfileVC

// MARK: - Set Fav Pet Name
    
    func setPetName(pet: String) {
        lblFavouritePet.text = pet
    }

// MARK: - Button Select Your Fav Pet Event

    @IBAction func btnSelectYourFavPet_Event(_ sender: UIButton) {
        
        let vc = FormsVC()
        
        self.present(vc, animated: true)
    }

below is a code for FormsVC:

// FormsVC

// MARK: - Variable Declaration
    
    var favoritePet = String()

// MARK: - viewDidLoad Method

    override func viewDidLoad() {
        super.viewDidLoad()

        setUpFormsVC()
        
    }

// MARK: - Set Up FormsVC
    
    func setUpFormsVC() {
       
        btnDog.titleLabel?.text = "Dog"
        btnCat.titleLabel?.text = "Cat"
        btnRabbit.titleLabel?.text = "Rabbit"
        btnBird.titleLabel?.text = "Bird"
        
    }

// MARK: - Button Selected Pet Event
    
    @IBAction func selectedPetEvent(_ sender: UIButton) {
        
        favoritePet = sender.titleLabel?.text ?? "Dog"
        
    }

// MARK: - Selected Pet Name
    
    func getFavoritePet() -> String {
        return favoritePet
    }

// MARK: - Button OK Event

    @IBAction func btnOk_Event(_ sender: UIButton) {
        
        let vc = ProfileVC()
        
        self.dismiss(animated: true, completion: {
            vc.setPetName(pet: self.getFavoritePet())
        })

// problem occurs when I dismiss FormsVC after selecting pet, the label displaying selected pet name (lblFavouritePet) throwing error of "Unexpectedly found nil while implicitly unwrapping an Optional value"
        
    }
}

Problem occurs when I dismiss FormsVC after selecting pet, the label displaying selected pet name (lblFavouritePet) throwing error of "Unexpectedly found nil while implicitly unwrapping an Optional value". I have no idea why it is found nil because I have assigned favoritePet's value of selected pet. Sorry for this dumb question, Could anyone help me ?


Solution

  • First of all, you have to declare the closure where you want to pass data.

    // FormsVC
    // MARK: - Variable Declaration
    let completion: ((String)->Void)? = nil
    
    // MARK: - Button OK Event
    @IBAction func btnOk_Event(_ sender: UIButton) {
        
        completion?(self.getFavoritePet())
        self.dismiss(animated: true)
    }
    

    The second part is you have to write the code to receive the data.

    // ProfileVC
    // MARK: - Button Select Your Fav Pet Event
    
    @IBAction func btnSelectYourFavPet_Event(_ sender: UIButton) {
        
        let vc = FormsVC()
        vc.completion = { petName in
            self.setPetName(pet: petName)
        }
    
        self.present(vc, animated: true)
    }