viewWillAppear:同时使用选项卡栏和导航控制器时方法似乎没有响应

发布于 2024-09-11 22:28:38 字数 286 浏览 5 评论 0原文

我正在使用基于选项卡栏+导航的应用程序,并且我有 4 个选项卡栏。当我从一个视图控制器导航到另一个视图控制器时,viewWillAppear: 方法似乎没有响应,我被迫通过创建下一个视图控制器的对象来手动调用它。所以我的问题是,每当我从一个视图控制器导航到另一个视图控制器时,如何避免手动调用 viewWillAppear: 方法?相反,它应该自动触发,就像您从一个视图控制器导航到另一个视图控制器时触发 viewdidLoad: 方法一样。请指导我该怎么做。

希望得到最好的答案 提前致谢

I am using Tab bar + navigation based application and I have 4 tab bars. When I navigate from one view controller to another view controller, the viewWillAppear: method doesn't seem to respond and I am being forced to call it manually by creating the object of the next view controller. So my question is, how do I avoid calling the viewWillAppear: method manually whenever I navigate from one view controller to another? Instead, it should get triggered automatically just like the viewdidLoad: method gets triggered when you navigate from one view controller to other. Please guide me on how could I do that.

Hoping for the best possible Answer
Thanks in Advance

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

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

发布评论

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

评论(6

青衫负雪 2024-09-18 22:28:38

你是对的, viewWillAppear 有点特殊,它通常会自动调用,但在某些情况下,包括当您手动添加视图控制器视图时(视图 addSubview:),以及将其作为视图控制器添加到 UITabBarController 时 或 UINavigationCnotroller (两者都有!)它不会收到消息。

然而,这仅适用于根视图,当您来回导航(可能使用导航控制器)时,该根视图的 viewWillAppear 将在某个时刻被触发。

简而言之,如果您需要在这些情况下在 viewWillAppear 中实现某些内容,那么当您知道它将被呈现时,您应该自己发送消息。您可以在视图控制器中处理这种情况,请查看以下有关此事的文章:

http://www.touchthatfruit.com/viewwillappear-and-viewdidappear-not-being-ca

祝你好运。

You are correct, viewWillAppear is a little special, it is usually called automatically but in some cases including when you are adding a view controllers view manually (view addSubview:), and also when adding this as a view controller to a UITabBarController or UINavigationCnotroller (of which you have both !) it doesn't get messaged.

This however is only for the root view, as you navigate (maybe with a navigation controller) back and forth, that root view's viewWillAppear will get triggered as some point.

In short, if you need to implement something in viewWillAppear in these cases, you should message it yourself when you know it's going to be presented. You can handle this case in your view controller, check out the following article about the matter:

http://www.touchthatfruit.com/viewwillappear-and-viewdidappear-not-being-ca

Good luck.

冷血 2024-09-18 22:28:38

您应该检查 UITabBarDelegate 然后查找方法:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

描述:当用户选择标签栏时发送给代理
项目。

You should check UITabBarDelegate then look for the method:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item.

Description: Sent to the delegate when the user selects a tab bar
item.

草莓酥 2024-09-18 22:28:38

在某些具有选项卡栏控制器的应用程序中,每个选项卡在添加视图之前还需要一个导航控制器:

[[[_mainTabController topViewController] navigationController] pushViewController:renewalScreen animated:YES];

In some apps that have a tab bar controller, each tab also needs a nav controller before a view can be added:

[[[_mainTabController topViewController] navigationController] pushViewController:renewalScreen animated:YES];
岁吢 2024-09-18 22:28:38

您的意思是选项卡栏上有 4 个选项卡项目,而不是“4 个选项卡栏”?如果您有一个带有选项卡项的选项卡栏,则当用户选择选项卡时,系统绝对应该调用 viewWillAppear: 方法。您可能还有其他问题导致了该问题。

Do you mean 4 tab items on a tab bar, rather than '4 tab bars'? If you have a tab bar with tab items, the viewWillAppear: methods absolutely should be called by the system as the tabs are selected by the user. You could have other issues that are causing the problem.

梨涡 2024-09-18 22:28:38

你没有忘记在某个地方调用 [super viewWillAppear] 吗?

You didn't forget to call [super viewWillAppear] somewhere?

妥活 2024-09-18 22:28:38

尝试改用 viewDidAppear 方法。

Try using the viewDidAppear method instead.

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