检测何时按下标签栏项目

时间:2015-11-20 23:00:00

标签: swift uitabbarcontroller uitabbar

我有一个根视图控制器,它没有被设置为我的故事板上任何视图控制器的自定义类。相反,我的所有视图控制器都像这样继承了这个类。

// RootViewController
class RootViewController: UIViewController, UITabBarDelegate { 

    // This is not getting executed on any of the view controllers
    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        print("ddd")
    }
}

// Subclassing it 
class TopStoriesViewController: RootViewController {

}

但是当在视图控制器上按下tabbaritem时,我似乎正在努力做某事,该视图控制器是rootviewcontroller的子类,即没有打印消息。

6 个答案:

答案 0 :(得分:65)

您不希望视图控制器的基类成为UITabBarDelegate。如果您这样做,所有视图控制器子类都将是标签栏代理。我认为你想要做的是扩展UITabBarController,如下所示:

class MyTabBarController: UITabBarController, UITabBarControllerDelegate {

然后,在该类中,覆盖viewDidLoad并在其中将委托属性设置为self:

self.delegate = self

注意:这是设置标签栏控制器委托。标签栏有它自己的委托(UITabBarDelegate),标签栏控制器管理它,你不允许更改。

所以,现在这个类都是UITabBarDelegate(因为UITabBarController实现了该协议)和UITabBarControllerDelegate,你可以根据需要覆盖/实现那些委托方法,例如:

// UITabBarDelegate
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    print("Selected item")
}

// UITabBarControllerDelegate
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
    print("Selected view controller")
}

我猜你可能对后者更感兴趣。查看文档以了解每个代表提供的内容。

最后,在您的故事板中(假设您使用的是故事板),在Identity Inspector中将标签栏控制器的类设置为MyTabBarController,您就可以了。

Swift 3/4

// UITabBarDelegate
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    print("Selected item")
}

// UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    print("Selected view controller")
}

答案 1 :(得分:39)

这样做会导致错误

  

更改标签栏控制器管理的标签栏的代理是   不允许

这就是我所做的,工作

  1. 在您UITabBarControllerDelegate中,您继承了viewDidLoad
  2. class MyClass: UIViewController, UITabBarControllerDelegate { func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) { let tabBarIndex = tabBarController.selectedIndex if tabBarIndex == 0 { //do your stuff } } override func viewDidLoad() { super.viewDidLoad() self.tabBarController?.delegate = self } }
  3. 中设置委托
  4. 添加功能
  5. 示例:

    <!DOCTYPE html>
    <html>
    
        <head> 
            <title> A4 - HTML Frames - Jainamkumar Patel </title>
        </head>
    
        <frameset cols = "20%,50%">
            <frame src = "frame1.html">
    
        <frameset rows = "30%,30%">
            <frame src = "frame2.html">
    
        <frameset cols = "25%,25%">
            <frame src = "frame3.html" name = "frame1">
            <frame src = " ">
    
            </frameset>
        </frameset>
     </frameset>
    
    </html>
    

答案 2 :(得分:6)

这是@ mbeaty的答案版本,带有更多上下文。它改编自我的fuller answer here

import UIKit

class MyTabBarController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // tell our UITabBarController subclass to handle its own delegate methods
        self.delegate = self
    }

    // called whenever a tab button is tapped
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

        if let firstVC = viewController as? FirstViewController {
            firstVC.doSomeAction()
        }

        if viewController is FirstViewController {
            print("First tab")
        } else if viewController is SecondViewController {
            print("Second tab")
        }
    }

    // alternate method if you need the tab bar item
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        // ...
    }
}

将此设置为IB中的选项卡视图控制器的自定义类。

enter image description here

替代解决方案

  • 只需在标签的视图控制器的viewDidLoad方法中执行某些操作即可。见this answer

答案 3 :(得分:2)

快速5

要检测是否已选择特定的TarBarItem,请在自定义TabBarController上使用它:

    class MainTabBarController: UITabBarController, UITabBarControllerDelegate {

        override func viewDidLoad() {
            super.viewDidLoad()

            self.delegate = self

        }


   func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

        let selectedIndex = tabBarController.viewControllers?.firstIndex(of: viewController)!
        if selectedIndex == 0 {
            print("first tab bar was selected") 
        } else {
            //do whatever
        }
    }

}

答案 4 :(得分:1)

快速5轻松享受方式

//MARK:- it will work in 
class TabBar: UITabBarController, UITabBarControllerDelegate {

}

代码

override func viewDidLoad() {
    super.viewDidLoad()

    self.selectedIndex = 1
    self.title = "Measure"

    self.delegate = self        
}

//MARK:-  UITabBarDelegate
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    print("Selected item")
}

//MARK:- UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    let selectedIndex = tabBarController.viewControllers?.firstIndex(of: viewController)!
    if selectedIndex == 0 {
        self.title = "History"
    }
    else if selectedIndex == 1{
        self.title = "Measure"
    }
    else if selectedIndex == 2 {
        self.title = "Setting"
    } else {
        //do whatever
    }
}

答案 5 :(得分:0)

我使用了这段代码,因为我需要知道选择了相同的视图控制器。

import UIKit

protocol CustomTabBarControllerDelegate {
    func onTabSelected(isTheSame: Bool)
}

class CustomTabBarController: UITabBarController, UITabBarControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
    }

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        (viewController as? CustomTabBarControllerDelegate)?.onTabSelected(isTheSame: selectedViewController == viewController)
        return true
    }
}


class Tab1ViewController: CustomTabBarControllerDelegate {
    func onTabSelected(isTheSame: Bool) { 
        //do something
    }
}


class Tab2ViewController: CustomTabBarControllerDelegate {
    func onTabSelected(isTheSame: Bool) { 
        //do something
    }
}