In my project i have three tabBar item home, notification and profile. And side menu controller has home, bookings, profile and logout. Side bar menu controller was done by using SWRevealViewController cocopods.
When i navigating side bar menu to home tab bar index was selected correctly and navigating properly. While navigating from bookings it navigates properly but again navigating home app gets crashed. And console output says Could not cast value of type 'UINavigationController' (0x10ef79420) to 'UITabBarController' (0x10ef79970).
Since bookings controller is custom view controller and remaining are tab bar controller. And when navigating to booking screen view controller tab bar should be hide and user tap again menu button and navigating to home or any other controller.
And crashed due to booking controller does not has tab bar index. So how can navigate without crash to custom controller and tabbar controller with selected index item.
Here is my screenshot:
My storyboard screenshot:
Here is the code which i have tried:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// tableView.deselectRow(at: indexPath, animated: true)
let row = indexPath.row
if row == 0{
let tabBarController = revealViewController().frontViewController as! UITabBarController
let storyboard = UIStoryboard(name: "Home", bundle: nil)
let obj = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let navController = UINavigationController.init(rootViewController: obj)
tabBarController.selectedIndex = (indexPath as NSIndexPath).row
tabBarController.tabBar.isHidden = false
self.revealViewController().pushFrontViewController(tabBarController, animated: true)
} else if row == 1{
let tabBarController = revealViewController().frontViewController as! UITabBarController
let storyboard = UIStoryboard(name: "Bookings", bundle: nil)
let obj = storyboard.instantiateViewController(withIdentifier: "BookingsViewController") as! BookingsViewController
let navController = UINavigationController.init(rootViewController: obj)
// tabBarController.selectedIndex = 1
// tabBarController.tabBar.isHidden = false
self.revealViewController().pushFrontViewController(navController, animated: true)
} else if row == 2 {
let tabBarController = revealViewController().frontViewController as! UITabBarController
let storyboard = UIStoryboard(name: "Profile", bundle: nil)
let obj = storyboard.instantiateViewController(withIdentifier: "profileViewController") as! profileViewController
let navController = UINavigationController.init(rootViewController: obj)
tabBarController.selectedIndex = (indexPath as NSIndexPath).row
tabBarController.tabBar.isHidden = false
self.revealViewController().pushFrontViewController(tabBarController, animated: true)
} else if row == 3 {
print(indexPath)
// Log out user from Firebase
AuthService.signOut(onSuccess: {
// Present the Sign In VC
// PrefsManager.sharedinstance.logoutprefences()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let signInVC = storyboard.instantiateViewController(withIdentifier: "signInViewController")
self.present(signInVC, animated: true)
// self.navigationController?.pushViewController(signInVC, animated: true)
}) { (errorMessage) in
ProgressHUD.showError(errorMessage)
}
}
}