How can I create an action by pressing Tapbar before switching to another controller? I found such a solution, but it does not work, the print is not displayed, but just a transition to another controller occurs

class MyViewController: UIViewController, UITabBarDelegate { func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { print("ТАЙМЕР ОСТАНОВЛЕН") } 

}


Yes, it was not added ... Added, but the crash happens enter image description here

enter image description here

    2 answers 2

    Looked at your project. To achieve what you want, you need to do the following:

    1. Delete Outlet tabBar from CounterViewController in Storyboard (in Connection Inspector)
    2. Return CounterViewController.swift to its original view:

       import UIKit class СounterViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } } 
    3. Create a class like MyTabBarController, inheriting it from UITabBarController:

       import UIKit class MyTabBarController: UITabBarController { override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { print("Timer stopped") } } 
    4. In the Storyboard TabBarController set the value of the Class MyTabBarController

    Must earn.

    UPDATE

    I answer your question in a comment about issuing an Alert before switching to other TabBarItems. It turned out quite perverted, in my opinion, the decision, but still, it will suddenly help:

     import UIKit class MyTabBarController: UITabBarController, UITabBarControllerDelegate { var shouldSelectAnotherItems = false override func viewDidLoad() { super.viewDidLoad() self.delegate = self } func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { // если это первый TabBarItem (Recents) if viewController == self.viewControllers?[0] { return true } // иначе, если нажали на какой-нибудь другой TabBarItem if self.shouldSelectAnotherItems { return true } else { let alert = UIAlertController(title: "Продолжить?", message: "Вы действительно хотите сделать ЭТО?", preferredStyle: .alert) let yesAction = UIAlertAction(title: "Да", style: .default) { _ in print("Да") // разрешаем переходить на другие TabBarItems self.shouldSelectAnotherItems = true // переходим на тот TabBarItem, который был нажат switch viewController { case (self.viewControllers?[1])!: self.selectedIndex = 1 case (self.viewControllers?[2])!: self.selectedIndex = 2 default: self.selectedIndex = 0 } } let noAction = UIAlertAction(title: "Нет", style: .default) { _ in print("Нет") } alert.addAction(yesAction) alert.addAction(noAction) self.present(alert, animated: true, completion: nil) return false } } } 
    • Thank you very much! Works. There was also such a question, if before the transition to the other controller to launch an alert, and so that when it is shown, the transition is not performed, but only by clicking on the consent in the alert. Because now the alert is shown after the transition to the other controller. What method can this be set? Thank you - Artur Skachkov
    • one
      See the updated answer. - BrottyS
    • Thank you so much! - Artur Skachkov

    Maybe you just forgot to specify the delegate for the tabBar?

     class MyViewController: UIViewController, UITabBarDelegate { @IBOutlet weak var tabBar: UITabBar! override func viewDidLoad() { super.viewDidLoad() self.tabBar.delegate = self } func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { print("ТАЙМЕР ОСТАНОВЛЕН") } } 
    • Yes, it was not added ... Added, but a crash happens - Artur Skachkov
    • Check whether you have correctly linked the UI element ... Wonderful by the logs, in the tabBar, you do not have UITabBar, but UITabBarItem ... - Che
    • At what point is the crash? Can you put a detailed code on GitHub, for example? - BrottyS
    • Yes, I renamed UITabBarItem to UITabBar because I cannot find how to create such an outlet. Crash happens immediately when the first controller is loaded, I don’t work with git, look at it pliz yadi.sk/d/6a9Gxw45zBYuZ - Artur Skachkov