问题
I have created 5 Tabs in my application. In Tab1 i have UITableView
. On didSelectRowAtIndexPath
i am navigating to another UIView in which I am showing my all 5 Tabs. And I also play song in that navigated view.
Now when I click Back button in navigation and i again go to my original view, i am able to call viewWillDisappear
(as expected and normal situation).
But when I click directly another tab then viewWillDisappear
is not called in the navigated View. Why this Happens??
I have just thought in a way that when I directly clicks the another Tab then the view in Tab1 will call viewWillDisappear. But the navigated view will not call that method.
So what could be possible solutions?? kindly give some hints...
回答1:
I think that you need to catch the event when you switch between tabs. When you switch from Tab1 to Tab2, as you expect, viewWillDisappear
of Tab1 will not be called. Instead, the viewWillAppear
of Tab2 will be called.
Else if you want to catch the event when you switch tabs, check this link.
回答2:
I got the viewWillDisappear to work by calling
self.definesPresentationContext = true
in viewDidLoad()
Otherwise, viewWillDisappear wasn't getting called. And this is what I have in it:
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.searchController.active = false
tableView.reloadData()
}
Hope this helps.
回答3:
That is because the you have created tabBarController and you are pushing it as a viewController from the mainView.
So whole TabBarController is treated as one viewController.
Hope this helps you.
回答4:
if you want to call this method create the nsnotification center object viewWillDisappear and when you want to call this method post this notification.
回答5:
That's how I got it to work.
// UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
print("Selected view controller")
// do stuff...
self.navigationController?.setNavigationBarHidden(false, animated: false)
return true
}
来源:https://stackoverflow.com/questions/7227266/how-to-call-viewwilldisappear-method-in-tabbar-application-having-navigations-al