Hi All, I am trying to code something that seems very simple but have not found any documentation online for my scenario. Here are the App Details:
-
Storyboard app built using Interface Builder and a Tab Bar Controller
-
There are 3 tabs and no Navigation Controller (No Navigation controller on purpose to make the App simple)
Tab 1 = Dashboard (ViewController) Tab 2 = Air Quality Map (AirQualityMapViewController) Tab 3 = Fire Map (FireMapViewController)
The issue is that I would like Users to be able to navigate to the Maps from the Dashboard when they touch different buttons on the Dashboard(disguised as an image) or use the Tabs.
Segues won't work since the new screen covers the tab bar at the bottom of the screen. I am looking for a solution that does not cause the tab bar to be hidden after the User is presented the new screen.
So I put the following code in the View Controller for Tab 1 (Dashboard) after the viewDidLoad method hoping it would work, but I get an error, "Swift runtime failure: Unexpectedly found nil while unwrapping an Optional value" when the code hits the line below:
Do I need to create a new class for the UITabBarController? If so how? Any advice would be appreciated as I am new to coding!!!
Line where the error occurred:
let tabBarController = appDelegate.window!.rootViewController as? UITabBarController
Here is the full function:
@IBAction func fireMapButton(_ sender: Any) {
print("Fire Map Button tapped")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let tabBarController = appDelegate.window!.rootViewController as? UITabBarController
tabBarController?.selectedIndex = 2
self.dismiss(animated: true, completion: nil)
}
just find the tabBarController that leads to FireMapViewController self.tabBarController
So, if I understand correctly what you look for, try this:
@IBAction func fireMapButton(_ sender: UIButton) { // It is often better to give the precise class even though it will have no effect here
print("Fire Map Button tapped")
let tabBarController = self.tabBarController
tabBarController?.selectedIndex = 2
self.dismiss(animated: true, completion: nil)
}