DShah
DShah

Reputation: 9866

How to call viewWillDisappear method in Tabbar Application having navigations also

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...

Upvotes: 4

Views: 3853

Answers (5)

Chris8447
Chris8447

Reputation: 306

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
    }

Upvotes: 0

C0D3
C0D3

Reputation: 6559

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.

Upvotes: 13

iOS
iOS

Reputation: 3616

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.

Upvotes: 3

Rahul Juyal
Rahul Juyal

Reputation: 2144

if you want to call this method create the nsnotification center object viewWillDisappear and when you want to call this method post this notification.

Upvotes: 1

Parth Bhatt
Parth Bhatt

Reputation: 19469

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.

Upvotes: 2

Related Questions