简体   繁体   English

从子视图向父视图添加视图

[英]add view to the parent view from a subview

I have my rootviewcontroller. 我有我的rootviewcontroller。 in that I load view A. View A containts a button to load view B. however, I don't want to add view B as a subview of view A. which happens if I do this: 在这种情况下,我加载视图A。视图A包含一个按钮来加载视图B。但是,我不想将视图B添加为视图A的子视图。

- (IBAction)loadViewB:(id)sender {

    if (self.viewB == nil) {
        self.viewB = [[NUMViewController alloc] initWithNibName:@"NUMviewController" bundle:Nil];
    } 

    [self.view addSubview:viewB.view];

}

I want to load viewB as a subview of my rootview. 我想将viewB加载为rootview的子视图。 so I can animate out viewA before animating in viewB. 因此我可以先为viewA设置动画,然后再为viewB设置动画。

but I'm not sure how I must approach this. 但我不确定该如何处理。 I'm guessing I need a method in my rootViewController to handle the adding and removing of the subview. 我猜想我的rootViewController中需要一个方法来处理子视图的添加和删除。 I would need to be able to call that method from within my viewA subview. 我需要能够从我的viewA子视图中调用该方法。

any ideas? 有任何想法吗? thanks! 谢谢!

try [self.view.superview addSubview:viewB.view] 尝试[self.view.superview addSubview:viewB.view]
But I don't think this is the best approach as it comes to MVC. 但是我不认为这是MVC的最佳方法。
What can you do is add viewA to self.view and viewB to self.view also. 您可以做的是将viewA添加到self.view并将viewB添加到self.view。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM