为UITabBarItem设置Action

时间:2017-03-08 20:11:03

标签: ios swift xcode uitabbar uitabbaritem

如何为action设置UITabBarItem?我能以某种方式将它从storyboard连接到代码吗?还是以编程方式?如果是这样,怎么办呢?

我的层次结构是UITabBarController - > UINavigationController - > UITabBarItem

对于这个noobish问题很抱歉,但我真的需要一个答案。我在网上找不到任何关于此事的内容。

使用Swift 3和Xcode 8

谢谢!

我厌倦了曼诺普森的答案(没有用):

class BarsViewController: UITableViewController,UISearchResultsUpdating,UISearchBarDelegate,UISearchDisplayDelegate,UITabBarControllerDelegate{

override func viewDidLoad() {
super.viewDidLoad()


self.tabBarController?.delegate = self

        }

func tabBarController(tabBarController: UITabBarController, didSelect viewController: UIViewController) {
            if viewController.tabBarItem.tag == 1 {
                self.tableView.setContentOffset(.zero, animated: true)
            }
        }
        }
                extension UITableView {
                func scrollToTop(animated: Bool) {
                    setContentOffset(CGPoint.zero, animated: animated)
                }
            }

silentBob答案对我不起作用:

class BarsViewController: UITableViewController,UISearchResultsUpdating,UISearchBarDelegate,UISearchDisplayDelegate,UITabBarControllerDelegate{


    override func viewDidLoad() {
        super.viewDidLoad()


    self.tabBarController?.delegate = self
}

func tabBarController(tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if viewController.tabBarItem.tag == 1 {
            tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: .Top, animated: true)
        }
    }

}

我也尝试了Tushar Sharma的答案,但也没有用。

我想要做的是当你在tableView中向下滚动时,你将点击当前Tabbaritem tableView将滚动到顶部就像插图一样。

3 个答案:

答案 0 :(得分:0)

基本上你这样做:

1)确保你的班级确认到UITabBarDelegate

2)在IB中为每个标签栏项目设置标签

3)实现didSelectItem方法,如下所示:

 class yourclass: UIViewController, UITabBarDelegate {
        func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
           if(item.tag == 1) {
       //your code for tab item 1
    }
    else if(item.tag == 2) {
       //your code for tab item 2
    }
        }
    }

答案 1 :(得分:0)

我认为您需要设置UITabBarControllerDelegate协议。试试这个:

class YourClass: YourSuperClass, UITabBarControllerDelegate {

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if viewController.tabBarItem.tag == 1 {
            self.tableView.setContentOffset(.zero, animated: true)
    }
}

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tabBarController?.delegate = self
    }
}

答案 2 :(得分:0)

如果您只想滚动到顶部,有一个内置系统选项,请尝试点击状态栏。

编辑:

斯威夫特3:

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(_ animated: Bool) {
        self.tabBarController?.delegate = self
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension UITableViewController : UITabBarControllerDelegate {
    public func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
    }

}

斯威夫特2:

tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: .Top, animated: true)


extension UITableViewController : UITabBarControllerDelegate {
    func tabBarController(tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: .Top, animated: true)
    }
}

哦,那是一顿美味的午餐:)

如果您希望在每个视图控制器上滚动到顶部,我可以使用UITableViewController的扩展程序,我还在玩它,但我认为您可以使用它。