Reputation: 2362
I have three UIViewControllers: MainViewController, CurledViewController and SecondayViewController.
On MainViewController I have one UIButton in the MainViewController that displays the CurledViewController through:
curled = [[CurledViewController alloc] init];
[curled setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:curled animated:YES];
From the documentation I am told:
A modal view presented using this transition is itself prevented from presenting any additional modal views.
Which prevents me from opening the SecondaryViewController when the CurledViewController is displayed in this way. What I would like to do is, on the selection of a UIButton in CurledViewController, close the curl and open the SecondaryViewController (whether it's a call from CurledViewController or MainViewController doesn't matter). Upon closing SecondaryViewController, I would like CurledViewController to be re-opened.
In the function attached to the UIButton in CurledViewController I have attempted this through:
- (void)showSecondary:(UIButton *)sender {
[self.parentViewController dismissModalViewControllerAnimated:YES];
SecondaryViewController *secondaryView = [[SecondaryViewController alloc] initWithNibName:@"Secondary" bundle:Nil];
[self presentModalViewController:secondaryView animated:YES];
...
}
but am still told,
Application tried to present a nested modal view controller while curled
How do I go about opening a new UIViewController in this fashion?
Thanks!
Upvotes: 2
Views: 1281
Reputation: 6420
The problem here is that your code for presenting the SecondaryViewController is still executing from the CurledViewController. One alternative approach to try would be to create a CurledViewControllerDelegate protocol. Make MainViewController the delegate of CurledViewController and call your delegate method from showSecondary.
In CurledViewController, your method might look like:
- (void)showSecondary:(UIButton *)sender {
[self.delegate dismissCurledViewController:self];
}
In MainViewController, your delegate method might look like:
- (void)dismissCurledViewController:(CurledViewController *)controller {
[self dismissModalViewControllerAnimated:NO];
SecondaryViewController *secondaryView = [[SecondaryViewController alloc] initWithNibName:@"Secondary" bundle:nil];
[self presentModalViewController:secondaryView animated:YES];
…
}
Edit
In order to keep the animations on both the dismissal and the presentation of the new modal view controller, you will need to introduce a delay that allows enough time for the first animation to complete. You can do this by invoking performSelector:withObject:afterDelay:
with an appropriate delay value. This is an error prone approach, however, as it assumes that the first animation will always have the same duration.
As Andrew Pouliot suggested in another question, You could also try to override viewDidAppear:
in your MainViewController, so that it looks for a flag to determine whether a SecondaryViewController should be presented. This would still use the delegate approach I mentioned above, but MainViewController would have the following differences:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if(showSecondaryViewController) {
SecondaryViewController *secondaryView = [[SecondaryViewController alloc] initWithNibName:@"Secondary" bundle:nil];
[self presentModalViewController:secondaryView animated:YES];
}
showSecondaryViewController = NO;
}
- (void)dismissCurledViewController:(CurledViewController *)controller {
showSecondaryViewController = YES;
[self dismissModalViewController:YES];
}
Upvotes: 1