Reputation: 11807
My app starts in view A, all the views have a tabbar with 4 buttons that let's you jump to A, B, C, D
what I currently have is this scenario:
if you navigate to either, A, B, C, D you go back to the stack bottom with all ones on top wiped out.
but I need it to behave like in this image, for example these navigation happened
1: A>1
2: B>3>7
3: C>2>5>6>8>7
4: D>9>1
now when I move from 1 to B, I go back to 7 which is the top of B
Question: I assume I need more than one navigation controller, right? also I put this in didFinishLaunchingWithOptions
function in appDelegate
, how to do more than one navigation controller programmatically? And what to use between A, B push or load view controller?
////////// NC
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainView = storyboard.instantiateViewControllerWithIdentifier("selectLocation") as! selectLocation // 1 Home
let searchView = storyboard.instantiateViewControllerWithIdentifier("2") as! Search // 1 Home
let friendsView = storyboard.instantiateViewControllerWithIdentifier("3") as! Friends // 1 Home
let meView = storyboard.instantiateViewControllerWithIdentifier("4") as! Me // 1 Home
var nav1 = UINavigationController()
nav1.viewControllers = [mainView]
nav1.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
var nav2 = UINavigationController()
nav2.viewControllers = [searchView]
nav2.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
var nav3 = UINavigationController()
nav3.viewControllers = [friendsView]
nav3.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
var nav4 = UINavigationController()
nav4.viewControllers = [meView]
nav4.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UINavigationBar.appearance().barTintColor = UIColor(red: 176.0/255.0, green: 190.0/255.0, blue: 105.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barStyle = UIBarStyle.BlackTranslucent
self.window!.rootViewController = nav1
self.window?.makeKeyAndVisible()
////////// NC
Upvotes: 0
Views: 3587
Reputation: 2916
You should have a general UINavigationController
in your AppDelegate which includes the general app NavigationController, inside that tab navigation controller you could add all the navigationcontrollers with their UIViewController
as rootviewcontroller.
This would be using a navigation controller for each tab which is going to handle the navigation inside of that path. If the user navigates between tabs, those tab will have the latests viewcontroller in that tab. To do this programatically I'd create the nav controller object and set the rootviewcontroller as the first in the stack.
You can initiate the nav for each tab this way.
private lazy var navigationController:UINavigationController? = {
let navigationController = UINavigationController(rootViewController: InitialViewController())
navigationController.setNavigationBarHidden(true, animated: false)
return navigationController
}()
then every user tap you need to push another UIViewController
self.navigationController?.pushViewController(vc, animated: animated)
that way you will be able to go back in the stack.
You change that rootViewController this way if you need it
self.navigationController?.setViewControllers([vc], animated: animated)
I'd consider a good practise to create a wrapper class which includes that app navigationController and the array of NavigationControllers and methods to push/set navigation in case you need it. Look this similar question
I hope it helped.
Upvotes: 1