Mikhail Sein Mikhail Sein - 1 month ago 16
iOS Question

Tab Bar : interaction between tabs

I have a main TabBarVC in app which comprises 2 tabs (2 VC).

One of the tabs is Map VC (show google map).
How it should be VCs are not connected with each other.

It happened, I need to send coordinates for show from First Tab to Second(map vc).

My first idea is Creating variable of MapVC class and just present it.
But I have TabBarVC and I must just change selectionIndex :

self.tabBarController?.selectedIndex = 2


or not?)

It works, I created static var (lat,lon) in MapVC class and before change selectedIndex set this static variable. After changing tab, call viewDidLoad func of mapVC and I can show location on map. Okay.

Problem: If I will change tab to First and press button(show location on Second tab), viewDidLoad of MapVC will not run because already was creating. I can't call any funcs of MapVC (second tab) because I don't create it, its just a tab that creates itself..

Delegates,Protocols...What can help me?

Thanks!

Answer

In the show action of FirstVC, get MapVC:

if let mapVC = self.tabBarController.viewControllers[1] as? YourViewController {
    // you can use public properties of MapVC here
}