0

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

stacks

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
DeyaEldeen
  • 10,847
  • 10
  • 42
  • 75

1 Answers1

1

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.

DeyaEldeen
  • 10,847
  • 10
  • 42
  • 75
TomCobo
  • 2,886
  • 3
  • 25
  • 43
  • good, but I put "private lazy...." snippet in the delegate? why not to create 4? I can see that you only created one, could you please provide a detailed example? this question still somehow confuses me... [add the 4 navigation controllers] to the code of the question. – DeyaEldeen Aug 21 '16 at 12:04
  • sorry, I'm struggling to make this work, read sections from iOS books, and still confused, I searched a lot and made a lot of experiment, and applied what is above and seen the link you posted and other links, aren't there any example on github about this? please try to help me i'm stuck, where to put the lazy var? i tried to put that var in each tab view did load. – DeyaEldeen Aug 21 '16 at 13:06
  • this what appears when i add the lazy var http://i.imgur.com/xkC9tpa.png – DeyaEldeen Aug 21 '16 at 13:13
  • I can't just write the entire example. lazy var for the navigationController was just an idea. I don't know your code structure. Graphically the idea would be http://stackoverflow.com/a/25790498/2000162 and the way you add the viewcontrollers to the navigationcontroller is this http://stackoverflow.com/a/27592135/2000162 – TomCobo Aug 21 '16 at 15:40
  • I have solved this, if anybody need the solution i will prepare a sample github project and link to it here. – DeyaEldeen Aug 22 '16 at 07:57