1

I switch to ChildViewController with the goChildViewController function in Parent View.

class ParentViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func goChildViewController() {
        DispatchQueue.main.async {
            self.navigationController?.view.layer.add(CATransition().popFromRight(), forKey: nil)
            self.navigationController?.pushViewController(ChildViewController(), animated: false)
        }
    }

    func needToAccessThisFunction() {
        // I need to call this function from ChildViewController
    }

}

I want to access the function "needToAccessThisFunction" from the ChildViewController.

class ChildViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //ParentViewController.needToAccessThisFunction()
    }

}

How can I do that?

4 Answers4

1

There is multiple solutions to call parent from child view controller.

One is to use delegate(which I suggest strongly) and HERE is the simple example for that.

And there is one more where you can directly call parent by adding below code in child view controller.

@IBAction func btnTapped(_ sender: Any) {
    if let parent = self.parent as? ViewController { //ViewController is a parent view controller here.
        parent.methodFromChild()
    }
}

and in ViewController you need to declare

func methodFromChild() {
    print("call from child")
}
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
0

You can do this, but it's not a good idea.

Your ChildViewController is now on the navigation stack, so you could call the parent function something like this:

class ChildViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let parent = self.navigationController?.viewControllers[0] as? ParentViewController {
            parent.needToAccessThisFunction()
        }
    }
}

This assumes ParentViewController is at the bottom of the stack (item [0]).

Mike Taverne
  • 9,156
  • 2
  • 42
  • 58
0
Declare protocol to your child controller like this
protocol ChildControllerDeledate: class {
   func callParentFunction()
} 

Then Implement the child protocol in Parent controller 

Extension ParentController: ChildControllerDeledate {

   func callParentFunction() {
     //Call your parent function/method from here
   }

}
DJ_Mobi90
  • 121
  • 4
0

You can use delegates for that:-

     protocol requiredMethods: class {
           func a()
           func b()
        }

        class FirstVC: UIViewController, requiredMethods {
            func a() {}
            func b() {}


        func goChildViewController() {
                DispatchQueue.main.async {

        let vc = SecondVC()
               vc.delegate = self
               self.navigationController?.view.layer.add(CATransition().popFromRight(), forKey: nil)
                    self.navigationController?.pushViewController(vc, animated: false)
                }
            }

        }

        class SecondVC: UIViewController {
            weak var delegate: requiredMethods?

            // to  call method a() self.delegate?.a())
            // to  call method b() self.delegate?.b())

        }
Manish_Nainwal
  • 301
  • 1
  • 5