Looked at your project. To achieve what you want, you need to do the following:
- Delete Outlet tabBar from CounterViewController in Storyboard (in Connection Inspector)
Return CounterViewController.swift to its original view:
import UIKit class СounterViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } }
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") } }
- 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 } } }