0

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 ?

SwiftNewbie
  • 285
  • 1
  • 9
  • 2
    In the line `let vc = ProfileVC()` that is not your existing ProfileVC. Please read http://www.programmingios.net/dont-make-a-new-instance-by-mistake/ – matt Jan 02 '23 at 12:53

2 Answers2

0

As @matt mentioned, you should take the presenting view controller, not create the new instance. It's stated in the tutorial you use:

if let vc = presentingViewController as? Profile...

Your app crashes, because you use storyboards, and lblFavoritePet is an @IBOutlet implicitly unwrapped optional, hence, you should initialize it from the storyboard. But you initialize it without using the storyboard, and the property remains nil.

So, don't make a new instance, use the code that is stated in the tutorial.

And follow the naming conventions.

corscheg
  • 61
  • 1
  • 5
  • if let vc = presentingViewController as? Profile... If I write this line then presenting controller doesn't get dismissed !! that's why I wrote let vc = ProfileVC(), now I know this is wrong. but this if let vc = presentingViewController as? Profile... also doesn't work in my case ! – SwiftNewbie Jan 03 '23 at 06:35
-1

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)
}
HangarRash
  • 7,314
  • 5
  • 5
  • 32
  • this works ! Thank you ! just one change let completion: ((String)->Void)? = nil this should be variable not constant. thank you again! – SwiftNewbie Jan 03 '23 at 06:37