Search code examples
iosiphoneuinavigationcontrollersegueuistoryboardsegue

How to do circular and complex navigations?


In my program have 6 view controllers.(in storyboard) Lets define 1,2,3,4,5,6.

1 is my main view.

I want to navigate like this(image bellow).Is it possible to do? give me a idea to do this navigation.

enter image description here


Solution

  • Yes, u can implement this. This is a simple navigation in iOS using NavigationController. you have six viewControllers 1, 2, 3, 4, 5, 6.

    to do this: First create a NavigationController and initialize it with ViewController 1 (ie a root View Controller).

    Now your navigationController behaves like a stack which contains all ur pushed view controller. NavigationController is only push and pop ur view controllers.

    So, every time when u want to navigation first check ur viewController is inside navigationController stack or not. If it is already in stack then pop to that controller, if not then push the same view controller. for this use following:

    In case ViewController3

    -(void)popToSelectedViewController
    {
        NSArray *vc=[self.navigationController viewControllers];
    
        ViewController3 *vc3=nil;
    
        for (int i=0; i<[vc count]; i++)
        {
            UIViewController *tempVC=[vc objectAtIndex:i];
            if([tempVC isKindOfClass:[ViewController3 class]])
            {
                vc=[vc objectAtIndex:i];
                break;
            }
        }
    
       if(vc3)
        {
           //If exists inside stack the pop
             [self.navigationController popToViewController:vc3 animated:YES];
        }
       else
       {
            //If not exists inside stack push ViewController3
            ViewController3 *vc3New= [[ViewController3 alloc]initWithNibName:@"ViewController3" bundle:nil];
            [self.navigationController pushViewController:vc3New animated:YES];
             [vc3New release];
       }
    
    
    }
    

    For initializing ur ViewController1 with navigationController:

    if using storyboard Embed ur initialViewController(ie viewController3) with UINavigationController. for this:

    Step1: open storyboard, and select ur initialViewController(ie viewController3).

    Step2: Go to Editor in menu -> Choose Embed In -> Select UINavigationController. this creates a navigationcontroller and initializes with viewController3 as rootViewController.

    if not using storyboard make property of vc3 (ViewController3) and applicationNavigationController (UINavigationController) in .h

    and in .m:

    got method "application... didFinishedLaunching...." in appDelegate and write:

    self.vc3=[[ViewController3 alloc]initWithNibName:@"ViewController3" bundle:nil];
        self.applicationNavigationController=[[UINavigationController alloc] initWithRootViewController:self.vc3];
      self.window.rootViewController=self.applicationNavigationController;
    [self.window makeKeyAndVisible];