简体   繁体   English

从超级视图中删除当前视图

[英]Remove current view from superview

Let's say I have a viewcontroller "ViewBViewController".假设我有一个视图控制器“ViewBViewController”。 In that viewcontroller I create an instance of the "ViewAViewController" and use the addSubView: method to display this ViewAViewController.在那个视图控制器中,我创建了一个“ViewAViewController”的实例并使用 addSubView: 方法来显示这个 ViewAViewController。 It then processes a bunch of information, and is now done.然后它处理一堆信息,现在完成了。 I want it to automatically get removed as a subview when it's done.我希望它在完成后自动作为子视图删除。

I was looking at the removeFromSuperview method, but can't seem to call that from within the viewcontroller whose view I'm trying to remove (my first instinct was [self.view removeFromSuperview], but that gets rid of the entire view, not just the subview I'm after).我正在查看 removeFromSuperview 方法,但似乎无法从我要删除其视图的视图控制器中调用它(我的第一直觉是 [self.view removeFromSuperview],但这摆脱了整个视图,而不是只是我追求的子视图)。

The only way I can think of is setting up a delegate protocol, and have View B take care of the unloading of View A on behalf of View A as its delegate.我能想到的唯一方法是设置一个委托协议,并让视图 B 代表视图 A 作为其委托来处理视图 A 的卸载。 However this approach seems a bit overkill.然而,这种方法似乎有点矫枉过正。 Am I missing an easier solution?我错过了一个更简单的解决方案吗?

Thanks in advance!提前致谢!

UIViewController does not respond to removeFromSuperview , because a UIViewController is not a UIView but a UIViewController . UIViewController没有响应removeFromSuperview ,因为UIViewController不是UIView而是UIViewController No surprises there.那里没有惊喜。 You can call removeSuperview on any view, such as the view associated to a view controller (here self ):您可以在任何视图上调用removeSuperview ,例如与视图 controller 关联的视图(此处为self ):

[self.view removeFromSuperview];

or if you just want to remove one subview:或者如果您只想删除一个子视图:

[mySubview removeFromSuperview];

or if your subview is a member of self (ie declared in the interface say):或者如果您的子视图是self的成员(即在界面中声明):

[self.mySubview removeFromSuperview];

Have you tried: setHidden: YES ?您是否尝试过: setHidden: YES

There are two basically correct solutions here:这里有两个基本正确的解决方案:

  • Use a navigation controller.使用导航 controller。 You can hide the navigation bar if you don't want it to be part of your interface.如果您不希望它成为界面的一部分,您可以隐藏导航栏。 Then you can dispose of the top view controller and its view from either view controller by calling然后,您可以通过调用处理顶视图 controller 及其从任一视图 controller 的视图

    // argument can be YES or NO, as you like [self.navigationController popViewControllerAnimated:YES];
  • Use a delegate call that tells the parent view controller to do something like使用委托调用告诉父视图 controller 执行类似的操作

    -(void)removeViewA { // remove the view from the view hierarchy [self.viewAController.view removeFromSuperview]; // dispose of the view controller so it doesn't leak. self.viewAController = nil; }

It's important to make sure that you don't leak the child view controller and its view.确保不泄漏子视图 controller 及其视图非常重要。

Either of these approaches works, but using a navigation controller seems more idiomatic to me.这些方法中的任何一种都有效,但使用导航 controller 对我来说似乎更习惯。

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

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