点击tabBarItem以显示视图控制器

时间:2017-06-29 01:37:57

标签: ios swift uitabbarcontroller action uitabbaritem

如果按两次tabBar项,我在这里有一个自定义命令scrollToTop。但现在我想为特定的tabBaritem添加一个函数(标签3让我们说....)

我如何结合我现在拥有的东西:

import UIKit

class TabBarController: UITabBarController,UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        guard let viewControllers = viewControllers else { return false }
        if viewController == viewControllers[selectedIndex] {
            if let nav = viewController as? UINavigationController {
                guard let topController = nav.viewControllers.last else { return true }
                if !topController.isScrolledToTop {
                    topController.scrollToTop()
                    return false
                } else {
                    nav.popViewController(animated: true)
                }
                return true
            }
        }

        return true
    }
}
extension UIViewController {
    func scrollToTop() {
        func scrollToTop(view: UIView?) {
            guard let view = view else { return }

            switch view {
            case let scrollView as UIScrollView:
                if scrollView.scrollsToTop == true {
                    scrollView.setContentOffset(CGPoint(x: 0.0, y: -scrollView.contentInset.top), animated: true)
                    return
                }
            default:
                break
            }

            for subView in view.subviews {
                scrollToTop(view: subView)
            }
        }

        scrollToTop(view: view)
    }

    var isScrolledToTop: Bool {
        if self is UITableViewController {
            return (self as! UITableViewController).tableView.contentOffset.y == 0
        }
        for subView in view.subviews {
            if let scrollView = subView as? UIScrollView {
                return (scrollView.contentOffset.y == 0)
            }
        }
        return true
}
}

这样(因此我可以对特定的tabBarItem进行特定操作):

func tabBarController(_ tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
    if tabBarController.selectedIndex == 0 {
        // present your first view controller
    }
    else if tabBarController.selectedIndex == 1 {
        // present your second view controller
    }
    else if tabBarController.selectedIndex == 2 {
        // present your third view controller
    }

}

感谢您的帮助......

2 个答案:

答案 0 :(得分:1)

试试这段代码。使用点击手势识别器。

class MyTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let tap = UITapGestureRecognizer()
        tap.numberOfTapsRequired = 2
        tap.addTarget(self, action: #selector(MyTabBarController.twiceTapped(sender:)))
        tabBar.addGestureRecognizer(tap)
    }

    func twiceTapped(sender: UITapGestureRecognizer) {
        // handle here
    }
}

答案 1 :(得分:0)

让我看看我是否正确:你想根据标签栏中选择的项目(第一,第二等)做一些事情。如果是这样,我认为您可以根据项目的标记使用它(如果您设置了委托):

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    switch item.tag {
    case 1:
        // your code
    default:
        // your code
    }
}

并且不要忘记在标签栏中设置每个itens的标签。