AZTabBarController
AZTabBarController copied to clipboard
I can't use self.presentingViewController to find the parent view controller
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.
@IBAction func storeBtn(_ sender: UIButton) {
let vc = self.presentingViewController as! MoreViewController
vc.phoneLabel.text = "1234"
self.dismiss(animated: true)
}
Error message:
2020-04-14 19:24:18.058780+0800 InsuranceMasterSales[26704:24913201] Could not cast value of type 'InsuranceMasterSales.SixItemTabbarViewController' (0x10e84d178) to 'InsuranceMasterSales.MoreViewController' (0x10e84bc30).
It seems that swift get confused about the presentingViewController. How to solve this question? Thank you.
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.
- using Notification Center
- delegation pattern
I personally recommend the delegation pattern as it is more typed, but that is up to you.
Implementation steps:
- create a protocol (that extends class) with your functions to receive the data.
- in the main controller conform to that protocol.
- add a property that is weak in your presented controller of that protocol called delegate.
- when the data changes, call the delegate’s function.
- before launching the controller, assign the controller’s delegate to self (main controller)
@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)
}