Presenting a view controller modally from an action sheet's delegate in iOS8 - iOS11

IosUikitUiactionsheetUialertcontroller

Ios Problem Overview


So I noticed that in iOS8 beta 3 (Update: still happens in iOS 11.2) on iPad, when attempting to present a view controller from within a delegate method of a UIActionSheet, "nothing" happens and a log message is output to the debug console, stating that presentation was attempted while transitioning an alert controller:

Warning: Attempt to present <UIViewController: 0x...> on <ViewController: 0x...> which is already presenting <UIAlertController: 0x...>

Ios Solutions


Solution 1 - Ios

Update: As of iOS 9 SDK, UIActionSheet is deprecated, so do not expect a fix regarding this issue. It is best to start using UIAlertController when possible.


The problem seems to come from Apple's switch to using UIAlertController internally to implement the functionality of alert views and action sheets. The issue is seen mostly on iPad and action sheets, because on iPad, action sheets are presented as a popover within a specified view, and what Apple does is travel the responder chain until it finds a view controller and calls presentViewController:animated:completion: with the internal UIAlertController. The problem is less obvious on iPhone and with alert views, because there Apple actually creates a separate window, an empty view controller and presents the internal UIAlertController on top of that, so it seems to not interfere with other presentation.

I have opened bug report for this issue: rdar://17742017. Please duplicate it and let Apple know this is a problem.

As a workaround, I recommend delaying the presentation until the next runloop, using the following method:

dispatch_async(dispatch_get_main_queue(), ^ {
    [self presentViewController:vc animated:YES completion:nil];
});

Solution 2 - Ios

You can try to do your job (presenting view controller) in

- (void)      actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex {}

instead of

- (void) actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex {}

as @LeoNatan said, "The problem seems to come from Apple's switch to using UIAlertController internally to implement the functionality of alert views and action sheets". So you must to wait the action sheet dismissed, then present the view controller you want.

@LeoNatan's solution just block the UI at main thread, so it'll also make sure the view controller will be presented after the action sheet was dismissed.

Solution 3 - Ios

unfortunately this code doesn't work for me, I think because my problem was not calling presentController method directly but in the prepareForSegue method so using

[segue destinationViewController]

I've noticed that if the segue is "push" kind all works correctly, but if it is "modal", just in ipad, i got that error.

Then I've found some new option in storyboard in the segue panel, and i sovled my problem choosing "Current context" for Presentation option

I hope this will be helpful for someone else... here is the screenshot about the option

enter image description here

Solution 4 - Ios

I had this same issue. I created a separate window for alerts and actionsheets in my appdelegate and presented the alerts on it. It worked for me!

   self.alertWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  // Override point for customization after application launch.
  self.alertWindow.backgroundColor = [UIColor clearColor];
  UIViewController *dummy = [[UIViewController alloc] init];
  [self.alertWindow setRootViewController:dummy];

You can present as :

[[myAppDelegate appDelegate].alertWindow makeKeyAndVisible];
  [[myAppDelegate appDelegate].alertWindow.rootViewController presentViewController:alertController animated:YES completion:nil];

Solution 5 - Ios

I fixed it in Swift 3 with the following code

  DispatchQueue.main.async {
            self.present(alertController, animated: true, completion: nil)
        }

Solution 6 - Ios

Issuing a

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

on

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 

before trying to present another modal view worked for me.

Solution 7 - Ios

use

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

instead of

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

Action view is presented above current VC so thats what causes warning/error. when didDismiss is called, action view is already dismissed, so no problems at all :))

Solution 8 - Ios

Try

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    // action sheet presentation
    // or modal view controller presentation
    // or alert view presentation
}];

Solution 9 - Ios

In iOS 8 Apple uses UIAlertController internally to implement the functionality of alert views and action sheets. So when you want to show a UIViewController modally after displaying UIActionSheet or UIAlertView in delegate method like

(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

and

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

you have to first dismiss UIAlertController as follows:

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
{
    UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    [vc dismissViewControllerAnimated:NO completion:^{
        
    }];
}

Now you can present a modal UIViewController in iOS 8.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionL&#233;o NatanView Question on Stackoverflow
Solution 1 - IosLéo NatanView Answer on Stackoverflow
Solution 2 - IosKjulyView Answer on Stackoverflow
Solution 3 - IosAchilleView Answer on Stackoverflow
Solution 4 - IosKaeyView Answer on Stackoverflow
Solution 5 - IosJoan CardonaView Answer on Stackoverflow
Solution 6 - IosSalvadorView Answer on Stackoverflow
Solution 7 - IoszurakachView Answer on Stackoverflow
Solution 8 - IosTimView Answer on Stackoverflow
Solution 9 - IosPankaj purohitView Answer on Stackoverflow