0

I am building a app in which I am doing this hierachy:

view1controller(calls presentmodalviewcontroller method)-->view2controller(calls presentmodalviewcontroller method)-->view3controller(calls presentmodalviewcontroller)--->view4controller

I know the concept that I can always switch back from one view to another view by using dismissmodalviewcontroller method. Like i can switch from view 4 to view 3 and from view 3 to view 2. My question is that how can I switch to random view controller ? like from view 4 to view 2 or from view 3 to view 1 ? ..and I am not using navigation controller. I know I am missing a concept. Can anyone throw a light on this concept .

user1374408
  • 321
  • 1
  • 4
  • 16

4 Answers4

1

From the reference guide UIViewController

If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

iTukker
  • 2,083
  • 2
  • 16
  • 16
1
    AppDelegate *delegate_app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
ABC:;
    NSUInteger iRan = arc4random() % [delegate_app.viewcontrollerArray count];

    if ([(UIViewController *)[delegate_app.viewcontrollerArray objectAtIndex:iRan] isEqual:self]) 
    {
        NSLog(@"self so not taken");
        goto ABC;
    }

    [self presentModalViewController:(UIViewController *)[delegate_app.viewcontrollerArray objectAtIndex:iRan] animated:YES];    

viewcontrollerArray is allocated and synthesized in AppDelegate with all view controllers you have.

Dhaval Panchal
  • 2,529
  • 1
  • 25
  • 36
0

If you want to go from third view to 1st view or simillar to that you can try following code:

[self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];
hypercrypt
  • 15,389
  • 6
  • 48
  • 59
Developer
  • 1,435
  • 2
  • 23
  • 48
0

I think you should only keep one modal present at once. Dismiss without animation the ones you don't want visible.

first modal:

[view1controller presentViewController:view2controller animated:YES]

move to second modal:

[presentingViewController dismissModalViewControllerAnimated:NO]
[presentingViewController presentViewController:view3controller animated:YES]

move to third modal:

[presentingViewController dismissModalViewControllerAnimated:NO]
[presentingViewController presentViewController:view4controller animated:YES]

move back to first modal:

[presentingViewController dismissModalViewControllerAnimated:NO]
[presentingViewController presentViewController:view1controller animated:YES]

If you definitely need multiple nested modal view controllers you'll have to dismiss them in reverse order and represent more than one at a time sometimes with only the last one being animated. So to go from 1 to 4 you'd need to present 2 and 3 without animation and then present the 4th animated. And going the other way dismiss 4 with animation and 3 & 2 without animation. In this sort of scenario you would be better off using a navigation controller imho (you don't need to have a visible navigation bar).

see this question for more info

Community
  • 1
  • 1
ader
  • 5,403
  • 1
  • 21
  • 26