How to call the viewWillDisappear method in a Tabbar application with navigation

I created 5 tabs in my application. In Tab1 , I have a UITableView . On didSelectRowAtIndexPath I switch to another UIView in which I show all 5 tabs. And I also play a song in this navigation view.

Now, when I click the Back button in the navigation, and again I return to my original view, I can call viewWillDisappear (as expected, as in the usual situation).

But when I click on another tab, viewWillDisappear not called in the navigation view. Why is this happening?

I just thought that when I click on another tab, the view in Tab1 calls viewWillDisappear . But the navigation view will not call this method.

So what are the possible solutions? kindly give some advice ...

+3
source share
5 answers

I think you need to catch an event when you switch between tabs. When you switch from Tab1 to Tab2 , as you expect, viewWillDisappear Tab1 will not be called. Instead, viewWillAppear Tab2 will be called.

If you want to catch an event when switching tabs, check this link .

+3
source

I got viewWillDisappear to work by calling

 self.definesPresentationContext = true 

in viewDidLoad ()

Otherwise, viewWillDisappear was not called. And this is what I have:

 override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) self.searchController.active = false tableView.reloadData() } 

Hope this helps.

+7
source

This is because you created a tabBarController and you click it as viewController from mainView.

Thus, the entire TabBarController is considered as one viewController.

Hope this helps you.

+2
source

if you want to call this method, create an object of the nsnotificationWillDisappear center object and when you want to call this method, publish this notification.

+1
source

This is how I earned it.

 // UITabBarControllerDelegate func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { print("Selected view controller") // do stuff... self.navigationController?.setNavigationBarHidden(false, animated: false) return true } 
0
source

All Articles