一次性删除2个子视图

发布于 2024-10-11 00:44:51 字数 961 浏览 3 评论 0原文

我正在尝试用一种方法删除两个视图控制器(已相互添加)。我已经在interfacebuilder中制作了视图。它们都有自己的 .h 和 .m 文件。

我所处的场景:

我有一个主菜单,其中导入了 view2 头文件。 在一种方法中,我将第二个视图添加到超级视图的顶部,就像这样,

view2ViewController * view2 = [[view2ViewController alloc] initWithNibName:@"view2ViewController" bundle:nil];
[self.view addSubview:view2.view];

然后在视图 2 中,我添加了视图 3 头文件,这样我就可以将视图 3 添加为视图 2 顶部的子视图。我有另一种方法,它再次连接到 UIButton 的界面生成器,因此按下按钮时,会在 view2 中调用一个方法,该方法以完全相同的方式在顶部添加视图 3,如下所示:

view3ViewController * view3 = [[view3ViewController alloc] initWithNibName:@"view3ViewController" bundle:nil];
[self.view addSubview:view3.view];

我想解决的问题: 我在视图 3 中有一个按钮,它应该删除视图 3....,然后它也应该删除视图 2,以便主屏幕可见。

如何才能实现这一目标?

到目前为止我所拥有的:

[self.view removeFromSuperview];

然而,这仅删除了视图 3...,但保留了视图 2。

需要修改什么以便我也可以删除视图 2?

感谢任何帮助。

I am trying to remove two viewcontrollers (that have been added on top of each other) with one method. I have made the views in interfacebuilder. they all have their own .h and .m files to go with it.

Scenario I am in:

I have a main menu which has the view2 header file imported.
In a method I add the second view on top of the superview like so

view2ViewController * view2 = [[view2ViewController alloc] initWithNibName:@"view2ViewController" bundle:nil];
[self.view addSubview:view2.view];

then in view 2 I have added the view 3 header file so i can add view 3 as a subview ontop of view2. i have another method which is connected again to interface builder to a UIButton so upon button press a method gets called in view2 which adds view 3 on top in exactly the same way like so:

view3ViewController * view3 = [[view3ViewController alloc] initWithNibName:@"view3ViewController" bundle:nil];
[self.view addSubview:view3.view];

What im trying to solve:
I have a button in view 3 which should remove view 3.... and then it should also remove view 2 aswell so the main screen is visible.

How can this be achieved?

What I have so far:

[self.view removeFromSuperview];

This however only removes View 3... but leaves view 2 in place.

What needs to be modified so that i can remove view 2 as well??

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女净妖师 2024-10-18 00:44:51

我通常会这样做,将 view2 和 view3 添加为主视图的子视图。然后,当按钮动作被触发时,子视图的添加和删除将由主视图的视图控制器执行。

对于快速破解,我认为您可以在按钮处理程序中尝试此操作。

[self.view removeFromSuperview];
[self.view.superview removeFromSuperview];

虽然我不确定你是否应该这样做。 :P

I would normally do this as adding view2 and view3 as subviews of the main view. Then when the button actions are triggered, the adding and removing of subviews will be executed by the main view's view controller.

For a quick hack, I think you can try this in your button handler.

[self.view removeFromSuperview];
[self.view.superview removeFromSuperview];

Though I'm not sure if you should be doing it. :P

梦罢 2024-10-18 00:44:51

这是你需要的吗?

    [[[self.view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

Is this what you need?

    [[[self.view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
戈亓 2024-10-18 00:44:51

我不知道你到底想做什么,但我的印象是推动一个新的视图控制器就是你想要的。如果您的应用程序中有 UINavigationController,则只需执行

[navigationController PushViewController:view2 aimated:YES]

要在按下按钮时返回主菜单,您应该定义一个委托协议看起来像这样::

@protocol View3ViewControllerDelegate
- (void)view3ControllerBackToMainMenuButtonPressed:(View3ViewController*)controller;
@end

然后,该协议由实际推送其他视图控制器的类实现。在实现中,您将弹出所有不想再显示的视图控制器。为此,您可以使用

[navigationController popToViewController:myMainMenuViewControllerAnimated:YES]

或者如果您的主菜单视图控制器实际上是根视图控制器:

[navigationController [navigationController popToRootViewControllerAnimated:YES] 这样,

只有一个类负责推送和弹出所有视图控制器并处理“返回主菜单”按钮。在这种情况下,建议使用如上所述的自定义协议来处理推送视图控制器的弹出。

希望有帮助!

I don't know what you're trying to do exactly, but I get the impression that pushing a new view controller is what you want. If you have a UINavigationController in your app, you'd just have to do a

[navigationController pushViewController:view2 aimated:YES]

To go back to the main menu when the button is pressed, you should define a delegate protocol that looks something like this::

@protocol View3ViewControllerDelegate
- (void)view3ControllerBackToMainMenuButtonPressed:(View3ViewController*)controller;
@end

This protocol is then implemented by the class that actually pushes the other view controllers. In the implementation, you'd pop all view controllers you don't want to be displayed anymore. To do this, you could use

[navigationController popToViewController:myMainMenuViewController animated:YES]

or if your main menu view controller is actually the root view controller:

[navigationController [navigationController popToRootViewControllerAnimated:YES]

That way only one class is responsible for pushing and popping all view controllers and handling that "Back to Main Menu" button. Using a custom protocol as described above is the recommended way to handle the popping of pushed view controllers in a scenario like this.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文