Application tried to present modally an active con

2019-01-27 02:13发布

问题:

I have searched for and found various q/a on this error, but have not been able to find any specific help for my issue (at least that my minimal experience allows me to understand).

I am loading a UIView from the app's main menu, which in turn has several button options (call it submenu). One of these goes back to the main menu without issue ([self dismissModalViewControllerAnimated:YES]; ). Another button loads a UITableView (separate view controller), which loads fine. However, I want a button within this UITableView to go back to the submenu. When I use the above mentioned code, it goes all of the way back to the main menu. I can't seem to find a way to create an action that goes back to the submenu UIView (submenu).

When I try to do a standard ['uitableviewcontrollername' presentModalViewController:submenuView animated:YES]; I get the error Application tried to present modally an active controller (I get the same error if I replace uitableviewcontrollername with self.

The error makes sense in that I understand that it is already active, but what I need help with is, what exactly is the proper way to do what I've described above? Thanks for your time.

回答1:

I believe the proper way to dismiss a modal view is to use delegation as defined in here.

In practice you define a method on the 'menu view' that will dismiss the active modal view by calling the usual :

- (void) notifytoclose {
    [self dismissModalViewControllerAnimated:YES];
}

You call it from the menu view but it will close the active modal on top of it. Then you define a delegate property on the modal table view controller, set it to the instance of the menu view, and in the method that closes the table view you call :

[delegate notifytoclose];

The use of [self dismissModalViewControllerAnimated:YES] to close the current instance is not always working although the circumstances are not clear to me. I also noticed differences between iPAd and iPhone behaviour.



回答2:

For my specific needs, the following did the trick for getting back from thetableview:

[self.mapLegendViewController dismissModalViewControllerAnimated:YES];

Gregory's point is noted though, and I recommend reading the link he made as it is interesting as to how iPhone/iPad each handle these actions.