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 ?
CodePudding user response:
First of all, 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.
CodePudding user response:
First of all, you have to declarer the closer 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)
}