为 UITabBarItem 设置操作 [英] Set Action for UITabBarItem

查看:22
本文介绍了为 UITabBarItem 设置操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为我的 UITabBarItem 设置 action ?我可以将它从 storyboard 连接到代码吗?或以编程方式?如果是这样,怎么做呢?

How can i set an action for my UITabBarItem ? Can i connect it from storyboard to code somehow? or programmatically? And if so how can it be done?

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

My hierarchy is UITabBarController -> UINavigationController -> UITabBarItem

很抱歉问这个菜鸟问题,但我真的需要一个答案.我在网上找不到任何关于此的信息.

Sorry for the noobish question but i really need an answer for this. And I couldn't find anything regarding this online.

使用 Swift 3 和 Xcode 8

Using Swift 3 and 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 会像 intagram 一样滚动到顶部.

i also tried Tushar Sharma's answer and it didn't work as well.

What Im trying to do is when you are scrolled down in the tableView you will tap the current Tabbaritem the tableView Will scroll to top just like intagram.

推荐答案

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

If you only wish to scroll to top, there is a builtin system option for this, try tapping on status bar.

斯威夫特 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)
    }
}

哦,那是一顿不错的午餐:)

Oh, that was a nice lunch :)

这是 UITableViewController 的一个扩展,如果你想在每个视图控制器上滚动到顶部,你可以使用它,我还在玩它,但我认为你可以使用它.

Here is an extension for UITableViewController you can use if you wish to scroll to top on every view controller, i'm still playing with it, but i think you can use it.

这篇关于为 UITabBarItem 设置操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆