Reputation: 100
I have a SignInVc as a starting point and a skip button. If user taps skip, e goes to home page and when he taps any button he is pushed to SignInVc.
The homeVC has TabBar, like its one of the 4 tab bar Vc's.
if let viewControllers = self.navigationController?.viewControllers {
for controller in viewControllers
{
if controller == (tabBarController?.viewControllers![0]){
print("FOUND IT")
}
print(controller)
}
}
While debugging with breakpoints, i can see the home page in navigationController?.viewControllers
But i cannot access it!!, the print is not executed. What should i use in the RHS of == ?
The plan is to push to the homeVC instead of print code.
EDIT:
I'm adding the screen shots of the debug below
Here i want to get to the view controller at Index 2
[
Upvotes: 0
Views: 1342
Reputation: 707
I am not sure whether am right,
logically declare a global variable,
var initiateHomePage: Bool?
// declare above or outside any swift file
For button actions
case 1
@IBAction func skipButtonTapped(_ sender: Any) {
initiateHomePage = true // should go homepage
}
case 2
@IBAction func anyButtonTapped(_ sender: Any) {
initiateHomePage = false // should go SignInVC
}
Atlast
while executing
if initiateHomePage == true {
// redirect to home page
// use this to redirect to tab bar
if let viewControllers = self.navigationController?.viewControllers {
for controller in viewControllers
{
if controller == (tabBarController?.viewControllers![0]){
print("FOUND IT")
}
print(controller)
}
}
// or use this
let ViewController:UIStoryboard = UIStoryboard(name: "Module", bundle: nil)
let tabBarController = ViewController.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = tabBarController
} else {
// redirect to SignINVC
}
Upvotes: 1
Reputation: 5076
I do not think that you have such hierarchy as you've described. Really you have
NavigationController -> TabBarController -> HomeViewController or
TabBarController -> NavigationController -> HomeViewController
Properties .navigationController
and .tabBarController
find nearest accessible Navigation and TabBar controller.
Just exam your hierarchy in storyboard or in code, and you will fix your problem.
UPD.
Based on your screen, you should find tabBar controller first, and find HomeViewController in tabBarController. I think, the code should looks like:
if let tabBar = navController.viewControllers.first(where: { $0 is UITabBarController} ){
let homeController = tabController.viewControllers?.first(where: { $0 is HomeViewController})
print("Home controller: \(home)")
}
Upvotes: 2