Reputation: 230
I have a typical ViewController:
class ViewController: UIViewController{
override func viewDidLoad() {}
}
And I also have TabBarController which displays me the number of the current scene:
class TabBarController: UITabBarController {
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem!) {
if item == (self.tabBar.items as! [UITabBarItem])[0]{
print(1)
}
else if item == (self.tabBar.items as! [UITabBarItem])[1]{
print(2)
}
}
}
I need my ViewController to know the number of its scene. (1 or 2). How can I deliver him this number for use from TabBarController?
Upvotes: 0
Views: 457
Reputation: 3064
What you want to do is, add a property called number
, or whatever you like, to your ViewController
class.
class ViewController: UIViewController {
var number: Int!
}
Then in tabBarController's viewDidLoad
, loop through the view controllers, check if they are of type ViewController
, and then set their number
property to the index of the tabBarController's view controllers:
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
for (index,vc) in (viewControllers!.enumerated())! {
if let viewController = vc as? ViewController {
vc.number = index
}
}
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem!) {
if item == (self.tabBar.items as! [UITabBarItem])[0]{
print(1)
}
else if item == (self.tabBar.items as! [UITabBarItem])[1]{
print(2)
}
}
}
Unless, are you wanting to set the value whenever the tab is selected?
Upvotes: 1