user9018881
user9018881

Reputation:

How to access function of ParentViewController

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?

Upvotes: 1

Views: 51

Answers (4)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71852

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

Upvotes: 1

Manish_Nainwal
Manish_Nainwal

Reputation: 301

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

        }

Upvotes: 0

DJ_Mobi90
DJ_Mobi90

Reputation: 139

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
   }

}

Upvotes: 0

Mike Taverne
Mike Taverne

Reputation: 9362

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]).

Upvotes: 0

Related Questions