单击选项卡栏上的选项卡时,获取回调/执行一些代码 [英] Get callback/execute some code when a tab on Tab Bar is clicked

查看:73
本文介绍了单击选项卡栏上的选项卡时,获取回调/执行一些代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或者,我可以使用诸如viewWillAppear之类的东西,只是切换选项卡不会调用viewWillAppear-如果我可以从那里可靠地访问selectedItem或selectedIndex.

Alternatively, I could use something like viewWillAppear, only switching tabs doesn't call viewWillAppear - IF I can access selectedItem or selectedIndex reliably from there.

目标是重新使用相似的表格视图,其中3个标签用不同的过滤数据填充表格.

The goal is to re-use a similar table view, with 3 tabs filling the table with differently filtered data.

我尝试覆盖didSelect并将应用程序委托用作UITabBarDelegate,但出现错误不允许更改由选项卡栏控制器管理的选项卡栏的委托."

I tried overriding didSelect and using the app delegate as UITabBarDelegate, but got the error 'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.'

选项卡栏控制器rootCt位于应用程序委托中,并且可以正常工作.

The tab bar controller, rootCt, is in the app delegate and works correctly.

这就是我要寻找的技巧-索引更改时从根(标签栏)控制器获取通知.想法?

So that's the trick I'm looking for - getting a notification from the root (tab bar) controller when the index has changed. Ideas?

推荐答案

我通常不做这种事情(回答我自己的问题).但是,去吧.

I don't usually do this kind of thing (answer my own question). But here goes.

例如,如果我单击选项卡2,然后单击选项卡3,然后再次单击选项卡2,则将调用该视图的viewWillAppear,但是didSelectViewController方法不是-并且selectedIndex不变!

For example, if I click tab 2, then tab 3, then tab 2 again, viewWillAppear for that view is called, but the didSelectViewController method is not - and selectedIndex is not changed!

它看起来好像selectedIndex仅在加载视图时才更新,而不是在视图已经加载并只是出现时更新.

It appears as if selectedIndex is only updated if a view is loaded, not if the view is already loaded and simply appears.

我进行了一些测试,并且与selectedIndex不同,即使视图已加载,选项卡栏的selectedItem也已正确更新(在viewWillAppear中为单击的选项卡中的视图).通过放置f.ex.标签选项卡中的标题,可以查找匹配的索引.

I did some testing, and unlike selectedIndex, the tab bar's selectedItem is correctly updated (in viewWillAppear for the view in the clicked tab) even if the view is already loaded. By putting f.ex. the tabs' titles in an array, the matching index can be looked up.

因此,我将省略didSelectViewController,并且不需要UITabBarController,我只需要将UITabBar连接到IBOutlet并使用[myTabBar selectedItem] .title在重新使用的视图的viewWillAppear中正确初始化.

So I will omit the didSelectViewController and won't need a UITabBarController, I only need to connect the UITabBar to an IBOutlet and use [myTabBar selectedItem].title to initialize correctly in the re-used view's viewWillAppear.

如果有人提供了一个更通用/有用/简单的解决方案,我会很乐意指出!几天后将再检查一次,如果没有,则将其标记出来.很高兴我成功了:)

If someone offers a more generic/useful/simple solution I'll gladly mark that! Will check back in a few days and mark this if not. Just glad I made it work :)

这篇关于单击选项卡栏上的选项卡时,获取回调/执行一些代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆