xcode viewDidUnload , viewWillDisappear 未在 tabBarContoller 中调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6569168/
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
viewDidUnload ,viewWillDisappear not called in tabBarContoller
提问by mrugen
I am making a view based application in which the first controller is viewcontroller there is login screen ,after login the next view is tabbar controller and I have 2 tabbar items on that tabbar .
Until this everything works fine . Now when i switch between these two views the viewWillDisappear
, viewDidUnload
is not called of previous tab clicked .
我正在制作一个基于视图的应用程序,其中第一个控制器是 viewcontroller 有登录屏幕,登录后下一个视图是标签栏控制器,我在该标签栏上有 2 个标签栏项目。直到这一切正常。现在,当我在这两个视图之间切换时viewWillDisappear
,viewDidUnload
不会调用之前单击的选项卡。
P.S.Even the viewwillAppear was not called ,which i called it with the Default Notification. Don't know what the issues are. Hope I am clear with my question.
PSE甚至没有调用viewwillAppear,我用默认通知调用了它。不知道是什么问题。希望我清楚我的问题。
回答by sergio
First of all, when switching view in a UITabBarController, the viewDidUnload
function is not called because the view is not actually unloaded. So, this is normal.
首先,在UITabBarController中切换视图时,该viewDidUnload
函数没有被调用,因为视图实际上并没有被卸载。所以,这是正常的。
What should work out of the box is viewWillAppear
/viewDidDisappear
. But there is a catch.
Depending on how you show your views, it might be that viewWillAppear
/viewDidDisappear
are not called by the framework for you. For example, this happens if you add your view as a subview, but there are more cases. I don't know how you display your tab bar, so cannot say anything more specific about it.
应该开箱即用的是viewWillAppear
/ viewDidDisappear
。但是有一个问题!根据您显示视图的方式,框架可能不会为您调用viewWillAppear
/ viewDidDisappear
。例如,如果您将视图添加为子视图,就会发生这种情况,但还有更多情况。我不知道你如何显示你的标签栏,所以不能说更具体的。
The easy solution I suggest to fix this is overriding the tabBarController:didSelectViewController:
selector in you tab bar controller delegate. From there you could implement you own logic or call viewDidDisappear
.
我建议解决此问题的简单解决方案是覆盖tabBarController:didSelectViewController:
选项卡栏控制器委托中的选择器。从那里你可以实现你自己的逻辑或调用viewDidDisappear
.
回答by Rakesh Bhatt
You should put your TabBar controller in MainWindow.xib.
您应该将 TabBar 控制器放在 MainWindow.xib 中。
First when you show loginscreen you will add your RootViewController like this:
首先,当您显示 loginscreen 时,您将像这样添加 RootViewController:
[self.window addSubview:self.rootview.view];
And when login is complete you can remove your RootViewController from mainwindow and add TabBarController in the mainwindow like this:
登录完成后,您可以从主窗口中删除 RootViewController 并在主窗口中添加 TabBarController,如下所示:
[self.rootview.view removeFromSuperview];
[self.window addSubview:self.tabBarController.view];
回答by ThomasW
Do you have a UINavigationController? You don't refer to one. If you aren't using a UINavigationController then it is likely that your UITabBarController isn't getting set up properly as the topViewController.
你有 UINavigationController 吗?你没有提到一个。如果您没有使用 UINavigationController,那么您的 UITabBarController 可能没有正确设置为 topViewController。