简体   繁体   English

标签栏上的iOS更改

[英]iOS on tab bar change

I want to know when the tab in the tab bar changes, so that i can report it to Firebase Analytics.我想知道标签栏中的标签何时发生变化,以便我可以将其报告给 Firebase Analytics。 How do i do this.我该怎么做呢。

I tried this我试过这个

override func viewDidAppear(_ animated: Bool) {
    Analytics.logEvent("projects_open", parameters: [:])
}

But i have a feeling that what would also run when i go back to it from another ViewController.但是我有一种感觉,当我从另一个 ViewController 返回它时也会运行什么。 I need something that can detect when a tab is opened, not when it becomes visible.我需要一些可以检测标签何时打开而不是何时可见的东西。

Is there another func that works for this?是否有另一个功能适用于此?

Swift 3.0斯威夫特 3.0

Use this two delegate methods, and don't forget to assign delegate to self.使用这两个委托方法,不要忘记将委托分配给self。

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    //MARK: - UITabBarControllerDelegate
}

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
   //MARk: - UITabBarDelegate
}

There is a delegate function on UITabbarController for detecting that a tab was selected: UITabbarController 上有一个委托函数用于检测选项卡是否被选中:

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)

You can also access the selected index like this:您还可以像这样访问选定的索引:

tabController.selectedIndex

If you use only tabBar.in viewDidLoad set tabBar delegate to self and如果您仅使用 tabBar.in viewDidLoad 将 tabBar 委托设置为 self 和

override func tabBar(_ tabBar: UITabBar, didSelect item:UITabBarItem) 
{
       //MARk: - UITabBarDelegate
if(tabBar.selectedIndex == 0) {
 //Do something
}
else if(tabBar.selectedIndex == 1) {
 //Do something.
} 
}

and if you use tabBarController use this method.And mark delegate as self如果您使用 tabBarController 使用此方法。并将委托标记为 self

func tabBarController(_ tabBarController: UITabBarController, 
didSelect viewController: UIViewController) {
}

Very important Note:非常重要的注意事项:

If you want to save which tabBar was previously selected you have to save it on your way.Either use flag or NSUserDefaults according to your wish.如果您想保存之前选择的 tabBar,您必须按自己的方式保存它。根据您的意愿使用标志或 NSUserDefaults。 The reason i mentioned this because i needed to check which tab has been selected right now in View in one of my project.我之所以提到这一点,是因为我需要检查现在在我的一个项目中的 View 中选择了哪个选项卡。

Swift 5斯威夫特 5

Easy way just click on link StakOverFlow screen will open https://stackoverflow.com/a/60539396/6881070简单的方法只需单击链接 StakOverFlow 屏幕将打开https://stackoverflow.com/a/60539396/6881070

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM