Swift:如何在按下UITabBarItem时执行操作 [英] Swift: How to execute an action when UITabBarItem is pressed

查看:342
本文介绍了Swift:如何在按下UITabBarItem时执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个连接到tableview控制器的标签栏控制器。当我按下标签栏项目时,我正试图进入tableview的顶部。我知道如何到达tableview的顶部。我只是不知道如何在按下项目时执行操作。

Currently I have a Tab Bar Controller that is connected to a tableview controller. I'm trying to go to the top of the tableview when I press the tab bar item. I know how to get to the top of the tableview. I just don't know how to do an action when the item is pressed.

推荐答案

您应该使用方法 UITabBarDelegate didSelectItem 。将它用作任何标准委托:

You should use UITabBarDelegate with method didSelectItem. Use it as any standard delegate:

class yourclass: UIViewController, UITabBarDelegate {
    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        //This method will be called when user changes tab.
    }
}

并且不要忘记将标签栏委托设置为在视图控制器中 self

And do not forget to set your tab bar delegate to self in view controller.

这篇关于Swift:如何在按下UITabBarItem时执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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