0

There are 3 view controllers View1,View2 and View3.

From view3 I have to navigate to view1.

I have tried the following code but it doesn't work.

//in View3.m
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    appDelegate.isComingFromCountries = YES;
    [self dismissViewControllerAnimated:YES completion:nil];

}
//in View2.m
-(void)viewWillAppear:(BOOL)animated
   {
  if (appDelegate.isComingFromCountries == YES)
    {

        appDelegate.isComingFromCountries = NO;
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    }

But this code doesn't work. How do I handle this?

Nayan
  • 3,014
  • 2
  • 17
  • 33
user2798258
  • 171
  • 1
  • 4
  • 13
  • If you are use Nvigation Controller that use [self.navigationController popToRootViewControllerAnimated:YES]; – h.kishan Jan 16 '14 at 09:14
  • I used this. CurrentLocationViewController *obj = [[CurrentLocationViewController alloc] init]; UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:obj]; [self.navigationController presentModalViewController:navBar animated:YES]; – user2798258 Jan 16 '14 at 09:19
  • [[[self parentViewController] parentViewController] dismissModalViewControllerAnimated:YES]; USE THIS MIGHT HELP – h.kishan Jan 16 '14 at 09:23

6 Answers6

2

You can use use presentingViewController for dismissing it,

try this -

[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES];

A -> B -> C

Running the above code in modal C will take you back to A.

Community
  • 1
  • 1
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
1

You have to use delay method to perform animation so that main thread should start performing in B viewController

[self performSelector:@selector(methodForDissmiss) withObject:nil afterDelay:0.5];

then write dismiss code in selector method to work by your logic.

Preetam Jadakar
  • 4,479
  • 2
  • 28
  • 58
0

You can try this one : if you are pushing one viewController to another and want to move back then use this one

[self.navigationController popViewControllerAnimated:YES];

if you are presenting viewController use modelly and want to move back then use this one

[picker dismissViewControllerAnimated:YES completion:nil];

also you can try this because first you need to dismiss third viewController then second one.

UIViewController *viewController = [self parentViewController];
    [self dismissModalViewControllerAnimated:NO];
    [viewController dismissModalViewControllerAnimated:YES];
Harunmughal
  • 367
  • 1
  • 4
0

If you are returning to the root view controller from a series of modally presented view controllers then the following code will work in iOS6

[self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];
codercat
  • 22,873
  • 9
  • 61
  • 85
0

I think, It may help you. According to your code( in comments area), You second view controller push from first view controller, then present third from second. So you have to set delegate as second in third. And do your code as below in third VC.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    appDelegate.isComingFromCountries = YES;
    [self dismissViewControllerAnimated:YES completion:^{
        [self.delegate dismissModal];
    };

}

In SecondVC.m

-(void)dismissModal
{ 
   [self.navigationController popViewControllerAnimated:NO];
}
Mani
  • 17,549
  • 13
  • 79
  • 100
0

Also you can pop to view 1 from view 3 on like this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

   [self.navigationController dismissViewControllerAnimated:NO completion:nil];
   [self.navigationController dismissViewControllerAnimated:YES completion:nil];

}
Shubham
  • 570
  • 3
  • 12