ios 如何从子视图控制器访问父视图控制器的视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8932313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can i access a parent view controller's view from a child view controller?
提问by jfisk
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.
我有一个主视图控制器负责绘制我的 2D opengl ES 视图,还有一个子视图控制器 buttonManager 确定在启动期间加载和绘制哪些按钮。
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];
}
回答by aqs
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
要访问父视图控制器,您可以使用self.parentViewController
. 拥有它后,您只需使用其view
属性即可访问其视图
回答by Jeremy C.
Note to those using iOS 5.x+
使用 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/
self parentViewController
现在返回nil
。您现在必须使用self presentingViewController
来达到相同的结果。有关升级代码库的更多信息和其他解决方法,请参阅此博客文章:http: //omegadelta.net/2011/11/04/oh-my-god-they-killed-parentviewcontroller/
回答by nahlamortada
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];
}
}
回答by Mohamed Hashem
Now after they have killed the
现在在他们杀死了
self.parent
you can use
您可以使用
override func didMove(toParentViewController parent: UIViewController?)
{
}
回答by Alfredo Papirriqui
I accomplish this using the objective c blocks approach. Take a look at this blocks tutorial. https://www.youtube.com/watch?v=FS4JAy1Wy3w
我使用objective c blocks方法来完成这个。看看这个块教程。 https://www.youtube.com/watch?v=FS4JAy1Wy3w