ios 检测以编程方式设置的 UITabBar Selected Index/Item Changes
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30692206/
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
Detect UITabBar Selected Index/ Item Changes that is set Programmatically
提问by JayVDiyk
I would like to know how do we detect when the selected TabBar Item or Index is changed whenthe changes is done programmatically?
我想知道当以编程方式完成更改时,我们如何检测选定的 TabBar 项目或索引何时发生更改?
self.tabBarController.selectedIndex = 1;
This two delegate function only detect changes when the tabBar Item was selected by user. It does not fire when the changes to the selectedIndex was done programmatically.
这两个委托函数仅在用户选择 tabBar 项目时检测更改。当对 selectedIndex 的更改以编程方式完成时,它不会触发。
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
println("tabBarController didSelectViewController")
}
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
println("tabBar didSelectItem")
}
回答by Eddy Liu
Previous answers are sufficient to "detect" the changes, however it does not detect which index is being pressed.
以前的答案足以“检测”更改,但是它不会检测正在按下哪个索引。
func selectItemWithIndex(value: Int) {
self.tabBarControllertabBarController.selectedIndex = value;
self.tabBar(self.tabBar, didSelectItem: (self.tabBar.items as! [UITabBarItem])[value]);
}
self.selectedIndexwill not return the selected index right away. To check which item is being pressed, we would need to compare the item with the tabBarItems in our UITabBarController
self.selectedIndex不会立即返回选定的索引。要检查哪个项目被按下,我们需要将项目与UITabBarController 中的 tabBarItems 进行比较
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
if item == (self.tabBar.items as! [UITabBarItem])[0]{
//Do something if index is 0
}
else if item == (self.tabBar.items as! [UITabBarItem])[1]{
//Do something if index is 1
}
}
回答by Meseery
Along all answers here, if you subclassed UITabBarController
already, here's a simple solution for all tab bar changes (User initiated and programmatic ):
沿着这里的所有答案,如果您UITabBarController
已经进行了子类化,这里是所有标签栏更改的简单解决方案(用户启动和程序化):
// Override selectedViewController for User initiated changes
override var selectedViewController: UIViewController? {
didSet {
tabChangedTo(selectedIndex: selectedIndex)
}
}
// Override selectedIndex for Programmatic changes
override var selectedIndex: Int {
didSet {
tabChangedTo(selectedIndex: selectedIndex)
}
}
// handle new selection
func tabChangedTo(selectedIndex: Int) {}
回答by Vineet Ashtekar
In swift, you can do it by overriding selectedIndex
property of UITabBarController
.
在SWIFT,您可以通过重写做到这一点selectedIndex
的财产UITabBarController
。
First subclass UITabBarController
and add all the following code.
第一个子类UITabBarController
并添加以下所有代码。
//Overriding this to get callback whenever its value is changes
override var selectedIndex: Int {
didSet {
handleTabSelection(selectedIndex: selectedIndex)
}
}
Also add this delegate method of UITabBarControllerDelegate
还要添加这个委托方法 UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
//Tab tapped
guard let viewControllers = tabBarController.viewControllers else { return }
let tappedIndex = viewControllers.index(of: viewController)!
//Tab tapped at tappedIndex
handleTabSelection(selectedIndex: tappedIndex)
}
Finally, we call this method from both places so that all the cases are handled.
最后,我们从两个地方调用这个方法,以便处理所有情况。
private func handleTabSelection(selectedIndex: Int) {
//Do something on tab selection at selectedIndex
}
回答by jeff-ziligy
Swift 3
斯威夫特 3
Here's a somewhat safer Swift-3 version, of what's been proposed above:
这是上面提出的更安全的 Swift-3 版本:
func selectItem(withIndex index: Int) {
if let controller = tabBarController,
let tabBar = tabBarController?.tabBar,
let items = tabBarController?.tabBar.items
{
guard index < items.count else { return }
controller.selectedIndex = index
controller.tabBar(tabBar, didSelect: items[index])
}
}
UITabBarController:
UITabBarController:
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if let items = tabBar.items {
items.enumerated().forEach { if == item { print("your index is: \(selectItem(withIndex: 1)
)") } }
}
}
Usage:
用法:
func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
toVC.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.fontFuturaMedium11, NSForegroundColorAttributeName: UIColor.colorAppThemeColor], for: .normal)
fromVC.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.fontFuturaBTBook11, NSForegroundColorAttributeName: UIColor.colorStudentTabText], for: .normal)
return nil
}
回答by Amit Saxena
that can't be done but some kind of hacking it can be done and i am sure that's gonna be a temporary solution for this problem. override below method for tabbarcontrollerdelegate do your stuff inside this is called when tab is switched in either way programatically or tapping on tab bar item.
这是无法完成的,但可以进行某种黑客攻击,我相信这将是解决此问题的临时解决方案。覆盖下面的TabbarControllerDelegate的方法DU DO您的内部在以双向单独或点击的选项卡项上以任何方式切换选项卡时调用此内容。
func selectItemWithIndex(value: Int) {
self.tabBarControllertabBarController.selectedIndex = value;
self.tabBar(self.tabBar, didSelectItem: (self.tabBar.items as! [UITabBarItem])[value]);
}
回答by Jonny
I only cared about the selectedIndex in one of the VCs in the tab bar controller stack. So I KVO:ed segmentedIndex on the tabbarcontroller instance from that VC. Works well, no real problems.
我只关心标签栏控制器堆栈中其中一个 VC 中的 selectedIndex。因此,我在该 VC 的 tabbarcontroller 实例上使用了 KVO:ed segmentedIndex。运行良好,没有实际问题。
回答by JayVDiyk
I've found a lovely solution to this. Thanks to Ckouta for the idea.
我找到了一个很好的解决方案。感谢 Ckouta 的想法。
I simply create a function to change the index and fire the delegate protocol of UITabBar's didSelectItem
我只是创建一个函数来更改索引并触发 UITabBar 的 didSelectItem 的委托协议
selectItemWithIndex(1);
Usage
用法
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item == (tabBar.items)![0] {
// do something with 1st tab
}
else if item == (tabBar.items)![1] {
// do something with 2nd tab
}
}
回答by V.Kumar
Swift 4:-You have to use these lines of code for detecting UITabBar
selected item index.
Swift 4:-您必须使用这些代码行来检测UITabBar
所选项目索引。