2

In my iPhone app, I have two views say VC1 and VC2. In VC1 I have a button to navigate to VC2. Here is the code in the button action in VC1:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (!appDelegate.vc2) {
    appDelegate.vc2 = [[VC2 alloc]initWithNibName:@"VC2" bundle:nil];
}
[self presentModalViewController:appDelegate.vc2 animated:NO];

(I have to keep some status in the VC2 thats why I declared VC2 in appdelegate file)

and in VC2, for going back I used [self dismissModalViewControllerAnimated:NO];

When I repeat the process of going to VC2 and returning using the button action there is no issue. But I also implemented push notification in my app. So when I get a push I send nsnotification on the Ok button click of Push notification alert view and the receive notification method is written in VC1 will receive the event, to navigate to VC2.

I used the same code as in above in receive notification method for navigating to VC2.

The second time that I try to navigate to VC2 with the Ok button click of alert view I am getting the error Application tried to present modally an active controller and I am having this error only in iPhone 4S (iO5). But its working fine if I repeat it using the button action in VC1.

Edit Adding code in detail:

In the appdelegate file, the action for Push notification alert view Ok button:

[[NSNotificationCenter defaultCenter] postNotificationName:@"VC1" object:nil];

And in the VC1 when receive notifications:

- (void)recieveNotification:(NSNotification *) notification{
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        if (!appDelegate.menuVC) {
            appDelegate.menuVC = [[MenuScreenVC alloc]initWithNibName:@"MenuScreenVC" bundle:nil];
        }
        [self presentModalViewController:appDelegate.menuVC animated:NO];
}

and button action for the button in VC1:

- (IBAction)viewMenuScreen:(id)sender{

            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            if (!appDelegate.menuVC) {
                appDelegate.menuVC = [[MenuScreenVC alloc]initWithNibName:@"MenuScreenVC" bundle:nil];
            }
            [self presentModalViewController:appDelegate.menuVC animated:NO];
}

and in VC2:

- (IBAction)backToVC1:(id)sender{
    [self dismissModalViewControllerAnimated:NO];
}
Mithuzz
  • 1,091
  • 14
  • 43
  • Show both the code in VC1 and VC2, and please show more of the relevant code. – uvesten Jun 26 '12 at 08:46
  • I have updated the question with more code. – Mithuzz Jun 26 '12 at 09:04
  • You can find the answer from the post http://stackoverflow.com/questions/8074766/application-tried-to-present-modally-an-active-controller/ – Srinivas Padidala Jun 26 '12 at 09:08
  • @SrinivasPadidala so should i use a delegate property for dismissing the view controller? I am not sure how to use it, can point what changes should I make in the above code? – Mithuzz Jun 26 '12 at 09:39

0 Answers0