KZY
KZY

Reputation: 91

How to call the controller when changing tab in tabbarcontroller? (Swift)

I have a search tab in the tab bar controller and a songs controller. I would like to jump to search tab when user click into one of the button in the songs controller and start searching. I am using this code to change tab

 self.tabBarController?.selectedIndex = 0

this above code works but i would like to call the search function in the search tab when changing tabs.

Upvotes: 1

Views: 1597

Answers (2)

Jithin
Jithin

Reputation: 943

Did you try calling your search function in viewWillAppear of your search view controller?

viewWillAppear will be invoked everytime you enter the viewcontroller in the tab bar.

Upvotes: 0

Reinier Melian
Reinier Melian

Reputation: 20804

You will be notified implementing the UITabBarControllerDelegate protocol in your ViewControllers

You need add this line in your viewDidLoad for your viewControllers

self.tabBarController?.delegate = self

and implementing this method of UITabBarControllerDelegate protocol

public func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)

You will be notified when a new viewController is selected in your tabBarController

Upvotes: 1

Related Questions