ios Swift:如何在按下 UITabBarItem 时执行操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30849030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:26:15  来源:igfitidea点击:

Swift: How to execute an action when UITabBarItem is pressed

iosswiftuitabbarcontroller

提问by K Swisha

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.

目前我有一个连接到 tableview 控制器的 Tab Bar Controller。当我按下选项卡栏项目时,我试图转到 tableview 的顶部。我知道如何到达 tableview 的顶部。我只是不知道按下该项目时如何执行操作。

回答by Nikita Zernov

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

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

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

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

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

回答by erparker

Here is an answer to this question

这是这个问题的答案

Basically you do this:

基本上你这样做:

  • Make sure your view controller is subscribed to the UITabBarDelegate
  • Set tags in IB for each tab bar item
  • Implement the didSelectItem method, something like this:

    -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
        if(item.tag == 1) {
           // Code for item 1
        }
        else if(item.tag == 2) {
           // Code for item 2
        }
    }
    
  • 确保您的视图控制器订阅了 UITabBarDelegate
  • 在 IB 中为每个标签栏项目设置标签
  • 实现 didSelectItem 方法,如下所示:

    -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
        if(item.tag == 1) {
           // Code for item 1
        }
        else if(item.tag == 2) {
           // Code for item 2
        }
    }
    

This will give you access to each tab item tapped event. Hope it helps!

这将使您可以访问每个选项卡项目点击事件。希望能帮助到你!

In Swift:

在斯威夫特:

func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    if(item.tag == 1) {
        // Code for item 1
    } else if(item.tag == 2) {
        // Code for item 2
    }
}

回答by Ahmed Safadi

SWIFT 3

斯威夫特 3

class yourclass: UIViewController, UITabBarDelegate {
    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        print("Test")
    }
}

And do not forget to set your tabBar delegate to self in viewDidLoad

并且不要忘记在 viewDidLoad 中将您的 tabBar 委托设置为 self

override func viewDidLoad(){
    super.viewDidLoad()
    <YOUR TAB BAR NAME>.delegate = self 
}

回答by Suragch

I was having trouble implementing the other answers here. This is a fuller answer. It assumes you are using a UITabBarController(the default if you create a new Tabbed App). This solution will print a message every time a view controller tab button is tapped.

我在这里实施其他答案时遇到了麻烦。这是一个更完整的答案。它假定您使用的是UITabBarController(如果您创建新的选项卡式应用程序,则为默认值)。每次点击视图控制器选项卡按钮时,此解决方案都会打印一条消息。

enter image description here

在此处输入图片说明

Code

代码

Create a new Swift file called MyTabBarController.swift. Paste in the following code.

创建一个名为MyTabBarController.swift的新 Swift 文件。粘贴以下代码。

import UIKit

class MyTabBarController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // tell our UITabBarController subclass to handle its own delegate methods
        self.delegate = self
    }

    // called whenever a tab button is tapped
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

        if viewController is FirstViewController {
            print("First tab")
        } else if viewController is SecondViewController {
            print("Second tab")
        }
    }
}

Interface Builder

界面生成器

On your storyboard select the Tab Bar Controller. Then in the Identity inspector, set the class name to MyTabBarController(that is, the name of the class in the code above).

在故事板上选择 Tab Bar Controller。然后在身份检查器中,将类名设置为MyTabBarController(即上面代码中的类名)。

enter image description here

在此处输入图片说明

That's all. You can run your app now and be notified whenever the user taps a tab bar item.

就这样。您现在可以运行您的应用程序,并在用户点击选项卡栏项时收到通知。

Notes

笔记

  • If you need to run a method on a tap, then you can do something like the following in didSelectmethod.

    if let firstVC = viewController as? FirstViewController {
        firstVC.doSomeAction()
    }
    
  • You could do make the FirstViewControllerimplement the delegate and handle everything there. That way you wouldn't need to make any custom UITabBarControllersubclass and set it in IB. However, having a child do the parent's work seems like the wrong place to do it. Anyway, here is is:

    class FirstViewController: UIViewController, UITabBarControllerDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            tabBarController?.delegate = self
        }
    
        func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
            // ...
        }
    }
    

    The didSelectmethod above gets called no matter which tab is tapped.

  • UITabBarControllerDelegatedocumentation

  • 如果您需要在水龙头上运行一个方法,那么您可以在didSelect方法中执行以下操作。

    if let firstVC = viewController as? FirstViewController {
        firstVC.doSomeAction()
    }
    
  • 您可以将FirstViewController实现作为委托并处理那里的所有内容。这样你就不需要创建任何自定义UITabBarController子类并将其设置在 IB 中。然而,让孩子做父母的工作似乎是错误的地方。无论如何,这里是:

    class FirstViewController: UIViewController, UITabBarControllerDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            tabBarController?.delegate = self
        }
    
        func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
            // ...
        }
    }
    

    didSelect上述方法被调用无论哪个选项卡被窃听。

  • UITabBarControllerDelegate文件

回答by Suragch

An alternate solution is to just do something in viewDidAppearin whichever View Controller the tab shows.

另一种解决方案是viewDidAppear在选项卡显示的任何视图控制器中执行某些操作。

First Tab View Controller

第一个选项卡视图控制器

import UIKit
class FirstViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("First tab")
    }
}

Second Tab View Controller

第二个选项卡视图控制器

import UIKit
class SecondViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("Second tab")
    }
}