检测标签栏项目何时被按下 [英] Detect when a tab bar item is pressed

查看:37
本文介绍了检测标签栏项目何时被按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根视图控制器,它没有设置为故事板上任何视图控制器的自定义类.相反,我所有的视图控制器都像这样继承了这个类.

I have a root view controller which isn’t set as the custom class for any of my view controllers on my storyboard. Instead, all of my view controllers are subclassing this class like so.

// RootViewController
class RootViewController: UIViewController, UITabBarDelegate { 

    // This is not getting executed on any of the view controllers
    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        print("ddd")
    }
}

// Subclassing it 
class TopStoriesViewController: RootViewController {

}

但是当在继承 rootviewcontroller 的视图控制器上按下 tabbaritem 时,我似乎很难做一些事情,即没有打印消息.

But I seem to be struggling with doing something when a tabbaritem is pressed on the view controller which is subclassing the rootviewcontroller, i.e, the message is not being printed.

推荐答案

您不希望视图控制器的基类是 UITabBarDelegate.如果你这样做,你所有的视图控制器子类都将是选项卡栏委托.我认为您想要做的是扩展 UITabBarController,如下所示:

You don't want your view controller's base class to be a UITabBarDelegate. If you were to do that, all of your view controller subclasses would be tab bar delegates. What I think you want to do is to extend UITabBarController, something like this:

class MyTabBarController: UITabBarController, UITabBarControllerDelegate {

然后,在该类中,覆盖 viewDidLoad 并将委托属性设置为 self:

then, in that class, override viewDidLoad and in there set the delegate property to self:

self.delegate = self

注意:这是设置标签栏控制器委托.标签栏有它自己的委托(UITabBarDelegate),由标签栏控制器管理,你不能改变.

Note: This is setting the tab bar controller delegate. The tab bar has it's own delegate (UITabBarDelegate), which the tab bar controller manages, and you are not allow to change.

所以,现在这个类既是 UITabBarDelegate(因为 UITabBarController 实现了那个协议),又是 UITabBarControllerDelegate,你可以根据需要覆盖/实现这些委托的方法,例如:

So, now this class is both a UITabBarDelegate (because UITabBarController implements that protocol), and UITabBarControllerDelegate, and you can override/implement those delegate's methods as desired, such as:

// UITabBarDelegate
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    print("Selected item")
}

// UITabBarControllerDelegate
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
    print("Selected view controller")
}

我猜你可能对后者更感兴趣.查看文档以了解每个代表提供的内容.

I'm guessing you're probably more interested in the latter. Check out the documentation to see what each of these delegates provide.

最后一件事,在您的故事板中(假设您正在使用故事板),将您的标签栏控制器的类设置为身份检查器中的 MyTabBarController,您就可以开始了.

Last thing, in your storyboard (assuming you are using storyboards), set your tab bar controller's class to MyTabBarController in the Identity Inspector, and you're good to go.

Swift 3/4

// UITabBarDelegate
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    print("Selected item")
}

// UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    print("Selected view controller")
}

这篇关于检测标签栏项目何时被按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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