Home > Blockchain >  How to pass delegate thru Navtigation Controller?
How to pass delegate thru Navtigation Controller?

Time:12-28

I have 2 VC's, and one navigation controller between them. How can i set First screen as a delegate of Second?

What i tried:

  1. Present SecondVC from frist (it presents it without navigation)
  2. Setting delegate in NavVC viewDidLoad()

FirstVC:

    class MainVC: UIViewController, SecondVCDelegate {
    func passData(text: String) {
        // do stuff
    }
    @IBAction func openNextVC(_ sender: Any) {
        let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavVC") as! NavVC
        present(nextVC, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

Navigation Controller:

class NavVC: UINavigationController {  
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

SecondVC:

    protocol SecondVCDelegate {
    func passData(text: String)
    }
    
    class SecondVC: UIViewController {
        var delegate: SecondVCDelegate?
    
        @IBAction func save(_ sender: Any) {
           // do stuff
        }
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    }

CodePudding user response:

Main task here would be to access second view controller instance from navigation controller instance(navC). And to achieve this, you have to first access the rootViewController from your navigation controller instance(nextVC) as below:

    let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavVC") as! NavVC
    let secondVC = nextVC?.viewControllers.first as? SecondVC
    secondVC.delegate = self 
    present(nextVC, animated: true, completion: nil)

The above code is applicable when second view controller is the root of your navigation controller. If second view controller is not the root controller to navC, then you have to iterate the controllers to get second view controller instance. Once second controller instance is there, then you can assign the delegate to the same as below.

    for controller in navC.viewControllers {
        if controller.isKind(of: SecondVC.self) {
           if let secondVc = controller as? SecondVC {
              secondVc.delegate = self
           }
           break
        }
    }
  • Related