I have a Parent UIViewController, which opens a child UIViewController:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("myChildView") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
I press a Button in the ChildView which should close the the ChildView and call a method in the Parent View:
self.dismissViewControllerAnimated(true, completion: nil)
CALL PARENTS METHOD ??????
How to do that ? I found a good answer (Link to good answer), but I am not sure if this is the best practice with UIViewControllers. Can someone help ?
Something that I've noticed in your question: Do not call a method (the parent method in your case) after dismissing the view controller. Dismissing the view controller will cause it to be deallocated. Later commands may not be executed.
The link that you've included in your question points to a good answer. In your case, I would use delegation. Call the delegation method in the parent view controller before you dismiss the child view controller.
Here is an excellent tutorial.
One easy way to achieve this you can use
NSNotificationCenter
for that.In your
ParentViewController
add this intoviewDidLoad
method:After that add this function in
ParentViewController
which will get called when you dismiss yourChildViewController
:and into your
ChildViewController
add this code where you dismissing your child view:Now when you dismiss child view
refreshList
method will be call.Add a weak property to the child view controller that should contain a reference to the parent view controller
Then in your code (I'm assuming it's inside your parent view controller),
And, as vomako said, call the parent's method before dismissing your child view controller.
Or, you can also call the method in the completion paramter of dismissViewControllerAnimated, where it will be run after you child view controller dismisses: