how can i access a parent view controller's vi

2019-02-08 03:45发布

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]; 
}

5条回答
成全新的幸福
2楼-- · 2019-02-08 04:26

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/

查看更多
对你真心纯属浪费
3楼-- · 2019-02-08 04:28

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

查看更多
淡お忘
4楼-- · 2019-02-08 04:36

I accomplish this using the objective c blocks approach. Take a look at this blocks tutorial. https://www.youtube.com/watch?v=FS4JAy1Wy3w

查看更多
趁早两清
5楼-- · 2019-02-08 04:39

that'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];

    }
}
查看更多
Lonely孤独者°
6楼-- · 2019-02-08 04:40

Now after they have killed the

self.parent

you can use

override func didMove(toParentViewController parent: UIViewController?)
{        
}
查看更多
登录 后发表回答