Navigating from UIAlertView/UIActionSheet in iOS8 (Xamarin.iOS) / by Pranav Khandelwal

With the release of iOS 8 came a whole new batch of API deprecations.

One of the major changes was the UIAlertController class replacing the UIAlertView and UIActionSheet as the preferred way of displaying alerts.

While updating an app for iOS 8, I ran into an NSInternalInconsistencyException and crash when trying to dismiss a view controller from a UIActionSheet:

Trying to dismiss UIAlertController <UIAlertController: 0x14de40020> with unknown presenter

Essentially what was occurring was that the view controller was dismissed before the UIActionSheet was dismissed; this threw the error above stating the “presenter” (the view controller that got dismissed) was “unknown”.

This is how I solved the issue:
I subscribed to the clicked and dismissed event on the action sheet:

MyActionSheet.Clicked += HandleMyActionSheetClicked;
MyActionSheet.Dismissed += HandleMyActionSheetDismissed; 


In my HandleMyActionSheetClicked method, I unsubscribe from the click event and then dismiss the action sheet, which, once dismissed, publishes the dismissed event, which invokes the HandleMyActionSheetDismissed method:

void HandleMyActionSheetClicked (object sender, UIButtonEventArgs e) 
{
    MyActionSheet.Dismissed -= HandleMyActionSheetClicked;
    ……
    if (e.ButtonIndex == MyActionSheet.CancelButtonIndex)
    {
        MyActionSheet.DismissWithClickedButtonIndex (MyActionSheet.CancelButtonIndex, true);
    }
}

In my HandleMyActionSheetDismissed method, I unsubscribe from the event and then dismiss the view controller:

void HandleMyActionSheetDismissed (object sender, UIButtonEventArgs e) 
{
    MyActionSheet.Dismissed -= HandleMyActionSheetDismissed;
    PresentingViewController.DismissViewController (true, null);    
}

This approach ensures that the UIActionSheet has been completely dismissed before it dismisses the view controller. I no longer get the NSInternalInconsistencyException or crash.

This exact same approach can be used with a UIAlertView.