Minitour / AZTabBarController

A custom tab bar controller for iOS written in Swift 4.2
MIT License
348 stars 65 forks source link

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

Open frank61003 opened 4 years ago

frank61003 commented 4 years ago

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 commented 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!

Minitour commented 4 years ago

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

frank61003 commented 4 years ago

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