I have a situation where child view controller is trying to display multiple view controller and while doing that child view controller needs to access play pause action method from the parent view controller. How this can be achieved that play pause action method which is to pause audio player, pause timer and pause layer:self.view.layer and is defined in parent view controller can be used by childviewcontroller.
I will appreciate so much for all kinds of help to solve this.
Thanks
You can access a view controller's parent with the
parentViewController
property.However, this depends on the relationship between your view controllers. From your question, I inferred that you had a parent-child relationship with multiple children, though please correct me if I am wrong! This is very different from a modal view controller presentation, in which only one view controller is presented and it demands the user's immediate attention.
Explanation:
There seems to be some confusion about the difference between the
parentViewController
andpresentingViewController
properties on UIViewController. There are two different view controller relationships, each of which applying to one of these properties.If you wish to add multiple view controllers' views as subviews of a parent view controller, you use view controller containment. In this situation, any of the views added as subviews (children) of the parent view controller will return the parent view controller (which controls the superview of the children; the parent view) when the
parentViewController
property is accessed. In this situation, thepresentingViewController
property returnsnull
.For example, in the parent view controller:
Contrarily, if you simply wish to present a single, modal view controller (a situation which is more common than the one-to-many parent-child relationship above), then the
presentingViewController
property is used.For example, in the presenting view controller:
Although
presentingViewController
might be seen more commonly due to the prevalence of the modal view controller pattern in iOS, the view controller containment parent-child relationship of view controllers is absolutely legitimate, and theparentViewController
andchildViewController
properties of a UIViewController have not been deprecated as of iOS 5, their use has just changed. You can read this excerpt from the documentation:You can access the Parent from the Child, even if the parent was
UINavigationController
ORUIViewController
using the following:and you can go upward like this
Each view controller has a property called
presentingViewController
(if ViewController1 presents modally presents ViewController2, ViewController1 is ViewController2'spresentingViewController
).Another option is to use
NSNotificationCenter
. You can then easily call the parent view controller's method from anywhere in the app.ParentViewController.m
ChildViewController.m
At
ParentViewController.h
At
ParentViewController.m
At
IBAction
of Play Button on child view controller do this: