Open frank61003 opened 4 years ago
presentingViewController is not the parent!
if you use:
if let vc = self.presentingViewController as? MoreViewController {
vc.phoneLabel.text = "1234"
self.dismiss(animated: true)
}
No crash because self.presentingViewController cannot be found.
You have to write:
if let vc = parent as? MoreViewController {
vc.phoneLabel.text = "1234"
self.dismiss(animated: true)
}
This is not relative to Swift at all!
@frank61003 to retrieve the data back from the controller you have two options.
I personally recommend the delegation pattern as it is more typed, but that is up to you.
Implementation steps:
@iDevelopper Thanks for your kindly explanation. I try to use parent but it still can't find the parent view controller. I provide the code how I present the view Controller. Do you have any idea about the issue? Thank you.
@IBAction func editSignatureLineBtn(_ sender: UIButton) {
let vc2 = self.storyboard!.instantiateViewController(withIdentifier: "EditSignatureLineViewController") as! EditSignatureLineViewController
vc2.phone = "22323077"
vc2.modalPresentationStyle = UIModalPresentationStyle.custom
vc2.modalTransitionStyle = .coverVertical
self.present(vc2, animated: true)
}
Here is the introduction about the presentingViewController https://developer.apple.com/documentation/uikit/uiviewcontroller/1621430-presentingviewcontroller
I think the problem is in vc2.modalPresentationStyle = UIModalPresentationStyle.custom. After I changing my code to vc2.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext. Below code can work well.
@IBAction func storeBtn(_ sender: UIButton) {
let vc = self.presentingViewController as! UINavigationController
let vc2 = vc.topViewController as! MoreViewController
vc2.phoneLabel.text = "9090"
self.dismiss(animated: true)
}
Hi I use the AZTabBarController to controller my tab bar. When I present a new view, It works well. I got an issue to bring the data back to the parent viewController. I use self.presentingViewController to get my parent view controller. but it crash at all. Below are my code in the childViewController.
Error message:
It seems that swift get confused about the presentingViewController. How to solve this question? Thank you.