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

56,233

Solution 1

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

Solution 2

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/

Solution 3

here's what worked for me:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSString * segueName = segue.identifier;
    if ([segueName isEqualToString: @"child-view"]) {
        ChildViewController * childViewController = (ChildViewController *) [segue destinationViewController];
        [self addChildViewController:childViewController];
    }
}

Solution 4

Now after they have killed the

self.parent

you can use

override func didMove(toParentViewController parent: UIViewController?)
{        
}
Share:
56,233
jfisk
Author by

jfisk

Updated on July 05, 2022

Comments

  • jfisk
    jfisk almost 2 years

    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]; 
    }
    
  • Aleksey Potapov
    Aleksey Potapov over 7 years
    In Swift 3.0 use self.parent "Yeah! Swift 3.0, %$^%!" (c) Breaking Bad
  • Kashif
    Kashif about 7 years
    @aqs : why is self.parent nil in ViewDidLoad ?
  • Hola Soy Edu Feliz Navidad
    Hola Soy Edu Feliz Navidad about 7 years
    The link isn't broken for me.
  • Hola Soy Edu Feliz Navidad
    Hola Soy Edu Feliz Navidad about 7 years
    @Kashif, just check the next answer.
  • Jose
    Jose about 6 years
    Not sure if it is because I'm creating the child view programmatically or the self.parent or self.presentingViewController no longer work, but the didMove method was the only solution that worked for me. (And that I'm calling the childViewController.didMove(toParentViewController: self) when adding the child view)