Ios 为UITabBarItem设置操作

Ios 为UITabBarItem设置操作,ios,swift,xcode,uitabbar,uitabbaritem,Ios,Swift,Xcode,Uitabbar,Uitabbaritem,如何为我的uitabaritem设置操作?我能以某种方式将它从故事板连接到代码吗?还是通过编程?如果是这样的话,怎么做呢 我的层次结构是UITabBarController->UINavigationController->UITabBarItem 对不起,我的问题很难说,但我真的需要一个答案。我在网上找不到任何与此相关的信息 使用Swift 3和Xcode 8 谢谢大家! 我厌倦了Mannopson的回答(不起作用): silentBob的回答对我不起作用: 我也尝试了图沙尔·夏尔马的答案,但

如何为我的
uitabaritem
设置
操作
?我能以某种方式将它从故事板连接到代码吗?还是通过编程?如果是这样的话,怎么做呢

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

对不起,我的问题很难说,但我真的需要一个答案。我在网上找不到任何与此相关的信息

使用Swift 3和Xcode 8

谢谢大家!

我厌倦了Mannopson的回答(不起作用): silentBob的回答对我不起作用: 我也尝试了图沙尔·夏尔马的答案,但效果并不理想。
我想做的是当你在
表格视图中向下滚动时,你会点击当前的
选项卡项
表格视图会像intagram一样滚动到顶部。

基本上你是这样做的:

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
    }
        }
    }

我想您需要设置
uitabarcontrollerdelegate
协议。试试这个:

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
    }
}

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

编辑:

Swift 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)
    }

}
Swift 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的一个扩展,如果您希望在每个视图控制器上滚动到顶部,您可以使用它,我仍在使用它,但我认为您可以使用它。

您是指UITableViewController吗?是的!很抱歉,我将修复此问题。请使用标记,然后您可以检测当前选择的选项卡栏项目。可以帮助我获取代码吗?我不知道我该怎么做…@Mannopson我真的很感激你的帮助谢谢你的帮助!我将在几个小时后进行测试并报告:)@Newbie Questions没问题请确保在IB中提供标记,否则如果您试图直接访问标记,它将崩溃。@Newbie Questions您在故事板中指定了标记吗?是的,我给每个TabbuttonItem指定了一个从1到4的特定选项卡,我不知道,目前无法检查。再过几个小时,我也许就能做到@塔沙尔萨马:知道为什么不起作用吗?谢谢你的帮助!我一到家就会试试这个。我在我的tableViewController代码文件中实现了这个。你在viewDidLoad中将委托设置为self吗?是的。代理设置为self您不需要任何其他滚动功能我知道,但instagram有两种方法,即点击状态栏和tabbaritem。。。。我也想要。在这里,我已经编辑了我的答案,我认为这应该适合你。此外,您还可以选择使用属性而不是标记来获取tabbar中的正确项。scrollRectToVisible也是一个选项。你能翻译成swift 2.2吗?我很难翻译这个。会有帮助的,谢谢。当然,但如果可以的话,我建议你换成Swift 3。它不会倒退,Swift 4已经在进行中。我认为语法是这样的:scrollToRowAtIndexPath(nsindepath(forRow:0,inSection:0),atScrollPosition:.Top,animated:true),我知道。。。可悲的是,我几乎不知道swift 2.2,这就是我开始使用的语法,我对这种语法最熟悉。。。。我需要一些帮助,将我的项目转移到Swift 4:(我试图通过你的答案和在线视频学习。谢谢你的翻译,我现在就试试
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)
    }

}
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)
    }
}