Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't use self.presentingViewController to find the parent view controller #64

Open
frank61003 opened this issue Apr 14, 2020 · 3 comments

Comments

@frank61003
Copy link

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.

@iDevelopper
Copy link

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!

@Minitour
Copy link
Owner

@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)

@frank61003
Copy link
Author

frank61003 commented Apr 15, 2020

@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)
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants