0

How can i go back 2 or 3 views back without using navigation controller? That is in my app, there is a main menu view. i want to reach that menu from all the other pages (from multiple views). How can this be implemented? with [self dismissModalViewControllerAnimated:YES]; this cannot be implemented i suppose. Anybody please help me..

sra
  • 23,629
  • 7
  • 52
  • 88
priya
  • 235
  • 4
  • 18

4 Answers4

2

"Going 2 views back without navigation controller"

Hmmm, I'm not sure if this misses the point, but the easiest way to do this is to use the popToRootViewControllerAnimated in the Navigation Controller:

[self.navigationController popToRootViewControllerAnimated:TRUE];

So, supposing you had a series of three screens in a Navigation Controller, and on the third screen you wanted the "Back" button to take you back to the initial screen.

On the third screen, you would add this code:

-(void)viewDidLoad
{
    [super viewDidLoad];

    // change the back button and add an event handler
    self.navigationItem.leftBarButtonItem =
    [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                     style:UIBarButtonItemStyleBordered
                                    target:self
                                    action:@selector(handleBack:)];
}


-(void)handleBack:(id)sender
{
    NSLog(@"About to go back to the first screen..");
    [self.navigationController popToRootViewControllerAnimated:TRUE];
}
Mike Gledhill
  • 23,658
  • 6
  • 133
  • 143
2

I found the solution.

Of course you can find the solution in the most obvious place so reading from the UIViewController reference for the dismissModalViewControllerAnimated method ...

If you present several modal view controllers in succession, and thus build a stack of modal 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.

so it's enough to call the dismissModalViewControllerAnimated on the target View. I used the following code:

[[[[[self parentViewController] parentViewController] parentViewController] parentViewController] dismissModalViewControllerAnimated:YES];

to go back to my home.

Copied from here

Community
  • 1
  • 1
Rakesh Bhatt
  • 4,600
  • 3
  • 23
  • 38
  • You could have marked the question as duplicate and provided the link in comment section. – viral Apr 16 '13 at 09:00
0
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];   
Zaraki
  • 3,540
  • 30
  • 39
0

Do you want to go "two or three VIEWS back"? Use removeFromSuperview? Or are you talking about ViewControllers?

Are you using Storyboard?

If yes do:

- (void)showModalAssistantViewController
{

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"AssistantStoryboard" bundle:nil];
AssistantRootViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"AssistantNavigationController"];
[viewController setModalPresentationStyle:UIModalPresentationFullScreen];
[viewController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.navigationController presentModalViewController:viewController animated:YES];
//... or pushToViewController ... whatever, you get the point.
}
DAS
  • 1,941
  • 2
  • 27
  • 38
  • not using storyboard or something. using simple views only forward movement is thru [self presentModalViewController:firstview animated:YES]; wanted to go back 2 and 3 views – priya Dec 29 '11 at 08:50