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];
}