ViewDidAppear 在基于选项卡栏的应用程序中未调用
我有一个问题。我正在开发一个基于选项卡栏的应用程序。在此应用中,当我们想要导航到新视图时,我们会调用 [self.view addSubview:newVC.view]
。 newVC 是我们要显示的新视图的视图控制器。当我们想返回到上一个视图时,我们还可以使用[self.view removeFromSuperview]
。
换句话说,没有导航控制器。现在的问题是我想更新以前的视图。由于我们使用 [self.view removeFromSuperview],前一个视图的 viewDidAppear
不会被调用,因此我们无法刷新该视图。
我知道我们使用的方法有缺陷,但由于它是一个大型应用程序,并将其更改为实现导航控制器需要花费大量时间,所以我需要您帮助我找到这个问题的解决方案。如何从其子视图调用 [self.view removeFromSuperview]
时调用 viewDidLoad
或 viewDidAppear
或上一个视图?
I have a problem. I am working on an app which is tab bar based app. In this app, we call [self.view addSubview:newVC.view]
when we want to navigate to a new view. newVC is the view controller of the new view that we want to display. Also we use [self.view removeFromSuperview]
when we want to go back to previous view.
So in other words, there is no navigation controller. Now problem is that I want to update the previous view. Since we are using [self.view removeFromSuperview]
, viewDidAppear
of the previous view is not get called and so we have no way of refreshing that view.
I know the approach that we used has flaw but since its a large scale app and changing it to implement navigation controller with take lot of time so I need you to please help me find the solution of this problem. How can I call the viewDidLoad
or viewDidAppear
or the previous view on calling [self.view removeFromSuperview]
from its subview?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,正如莎拉所说,您应该在“堆栈”中保留对先前控制器的引用。
当从堆栈中“弹出”控制器时,调用前一个控制器上的适当方法。
当然你不应该调用viewDidLoad(当你从真实的
UINavigationController
的导航堆栈中弹出控制器时它不会被调用)。您可以调用
viewWillAppear
或viewDidAppear
,但最好使用您自己的方法,例如 viewRevealed(您也可以从viewWillAppear
或viewDidAppear 调用它)。制作起来很有用
基类实现所有这些功能并从基类派生所有控制器。它可能看起来像:
Yes, as Sarah said you should hold a reference to previous controller in "stack".
And when "poping" controller from stack, call appropriate method on previous controller.
Certainly you should not call viewDidLoad (it is not called when you pop controller from navigation stack of real
UINavigationController
).You can call
viewWillAppear
orviewDidAppear
, but better use your own method, like viewRevealed (you can also call it fromviewWillAppear
orviewDidAppear
). It is useful to makebase class where implement all this functionality and derive all you controller from the base class. It may look like:
viewDidLoad
方法仅在您通过pushViewController
方法跳转到控制器时调用。如果您调用removeFromSupreView
,它将调用viewWillAppear
方法。在这里,如果您想通过选项卡栏从一个视图导航到另一个视图,则必须在Mainwindow.xib
中使用UINavigationController
并将其 viewController 与 App delegate 连接。viewDidLoad
method call only when when you jump into a controller throughpushViewController
method. If you callremoveFromSupreView
, it will callviewWillAppear
method. Here if you want to navigate from one view to another view over tabbar you must useUINavigationController
inMainwindow.xib
and connect its viewController with App delegate.