单击选项卡栏上的选项卡时获取回调/执行一些代码
或者,我可以使用类似 viewWillAppear 的东西,只有切换选项卡不会调用 viewWillAppear - 如果我可以从那里可靠地访问 selectedItem 或 selectedIndex 。
目标是重复使用类似的表视图,其中 3 个选项卡用不同的过滤数据填充表。
我尝试覆盖 didSelect 并将应用程序委托用作 UITabBarDelegate,但收到错误“不允许更改由选项卡栏控制器管理的选项卡栏的委托。”
选项卡栏控制器 rootCt 位于应用程序委托中并且工作正常。
这就是我正在寻找的技巧 - 当索引更改时从根(选项卡栏)控制器获取通知。有想法吗?
Alternatively, I could use something like viewWillAppear, only switching tabs doesn't call viewWillAppear - IF I can access selectedItem or selectedIndex reliably from there.
The goal is to re-use a similar table view, with 3 tabs filling the table with differently filtered data.
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.'
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试实现 UITabBarController 委托。它有一个类似于 UITabBar delegate 提供的 didSelect: 方法的方法:
tabBarController:didSelectViewController:
。它将在用户选择另一个选项卡后被调用。请参阅:UITabBarControllerDelegate 协议参考
Try implementing an UITabBarController delegate. It has a method similar to the didSelect: method offered by UITabBar delegate:
tabBarController:didSelectViewController:
. It will be called after the user has selected another tab.See: UITabBarControllerDelegate Protocol Reference
它将为您提供当前选定的选项卡索引的标签,
如果您使用 tabBar,
den是选择选项卡时调用的委托方法。
快乐的i编码...
It will give you the tag of current selected tabbar index
If you are using tabBar den
is the delegate method that gets called when tabbar is selected.
happy iCoding...
您可以使用
UITabBarControllerDelegate
的委托方法,并且
self.selectedIndex
将为您提供所选索引。You can use the delegate method of
UITabBarControllerDelegate
and in that you have
self.selectedIndex
will give you the selected index.我通常不做这种事情(回答我自己的问题)。但就这样吧。
例如,如果我单击选项卡 2,然后单击选项卡 3,然后再次单击选项卡 2,则会调用该视图的 viewWillAppear,但不会调用 didSelectViewController 方法 - 并且 selectedIndex 不会更改!
看起来好像 selectedIndex 仅在加载视图时更新,而不是在视图已加载并简单显示时更新。
我做了一些测试,与 selectedIndex 不同,即使视图已加载,选项卡栏的 selectedItem 也会正确更新(在单击选项卡中的视图的 viewWillAppear 中)。通过将 f.ex.标签的标题在数组中,可以查找匹配的索引。
因此,我将省略 didSelectViewController 并且不需要 UITabBarController,我只需要将 UITabBar 连接到 IBOutlet 并使用 [myTabBar selectedItem].title 在重用视图的 viewWillAppear 中正确初始化。
如果有人提供更通用/有用/简单的解决方案,我会很乐意标记!几天后会回来查看,如果没有的话请标记。很高兴我成功了:)
I don't usually do this kind of thing (answer my own question). But here goes.
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!
It appears as if selectedIndex is only updated if a view is loaded, not if the view is already loaded and simply appears.
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.
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 :)