Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i access a parent view controller's view from a child view controller?

Tags:

I have a main view controller that takes care of the drawing for my 2D opengl ES view, and a child view controller buttonManager that determines what buttons to load and draw during launch.

Once the user presses on one of these buttons, this view controller is created and its view is supposed to come up, but the view never gets added but has been tested to work. Heres my code from the main view controller:

 buttonManager=[[ButtonManager alloc] init];
 [self addChildViewController:buttonManager];
 [self.view addSubview:buttonManager.view];

and heres my code to launch this view:

-(void)launchStopDialog: (NSString*)stopName {
    NSLog(@"stopdialog should be launched.");
    if (stopDialogController == nil)
        stopDialogController = [[StopDialogController alloc] initWithNibName:@"StopDialog" bundle:nil];
    if (stopDialogController)
        [stopDialogController presentWithSuperview:self.view.superview withStopName:stopName]; 
}
like image 351
jfisk Avatar asked Jan 19 '12 19:01

jfisk


People also ask

How do I get parent view controller?

If you need to find the view controller that is responsible for a particular view, the easiest thing to do is walk the responder chain. This chain is built into all iOS apps, and lets you walk from one view up to its parent view, its grandparent view, and so on, until it reaches a view controller.

How do I embed a view controller in a navigation controller storyboard?

Step 1: Embed root view controller inside a navigation controller. In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .


2 Answers

To access the parent View controller you can use self.parentViewController. Once you have it you can access its view simply by using its view property

like image 186
aqs Avatar answered Sep 19 '22 11:09

aqs


Note to those using iOS 5.x+

self parentViewController now returns nil. You will now have to use self presentingViewController to achieved the same result. See this blog post for more information and additional work arounds for upgrading your code base: http://omegadelta.net/2011/11/04/oh-my-god-they-killed-parentviewcontroller/

like image 33
Jeremy C. Avatar answered Sep 19 '22 11:09

Jeremy C.