Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show alert message from UIViewController using UIAlertController

I'm trying to display message using UIAlertController in my UIViewController. Using VisualStudio 2015 CTP 5...

Used samples from:

http://forums.xamarin.com/discussion/26404/uialertcontroller-question-for-ipad http://www.hjerpbakk.com/blog/2014/11/23/from-uialertview-to-uialertcontroller-using-xamarin-and-async-await https://gist.github.com/Sankra/622e5855f95189e13d77

Based on samples above I have this so far:

public partial class MyViewController : GenericViewController //inherits from UIViewController 
{
   .....
     public async override void ViewDidLoad()
     {
            try
            {

                base.ViewDidLoad();
                //other irrelevant code here
                throw new Exception("Something went wrong");
            }
            catch (Exception ex)
            {
                    int result =   await AlertViewControllerHelper.ShowAlertDialogAsync(this, ex.StackTrace, true); 
            }
     }  
      ........
}

My static helper class:

public static class AlertViewControllerHelper 
{
public static Task<int> ShowAlertDialogAsync(UIViewController parent, string stackTrace, bool debugMode = false)
{

    var taskCompletionSource = new TaskCompletionSource<int>();

    try
    {
        var alert = UIAlertController.Create("Error", stackTrace, UIAlertControllerStyle.ActionSheet);
        if (alert.PopoverPresentationController != null)
        {
            alert.PopoverPresentationController.SourceView = parent.View;
            alert.PopoverPresentationController.SourceRect = parent.View.Bounds;
        }

        alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default,
            a => taskCompletionSource.SetResult(0)));
        if (debugMode)
        {
            alert.AddAction(UIAlertAction.Create("Info", UIAlertActionStyle.Default,
                a => taskCompletionSource.SetResult(1)));
        }

        parent.PresentViewController(alert, true, null);

    }
    catch (Exception ex)
    {               
    }
    return taskCompletionSource.Task;
}

After running the code my error dialogue is not is being displayed. I've completed an example using UIAlertView but so far no luck with UIAlertController (and that's my requirement)

Thanks in advance...

like image 323
Milen Avatar asked May 14 '26 21:05

Milen


1 Answers

Found solution for this:
1. Pass ParentViewController instead of the current view controller (this)

public partial class MyViewController : GenericViewController //inherits from UIViewController 
{

  .....
 public async override void ViewDidLoad()
 {
        try
        {

            base.ViewDidLoad();
            //other irrelevant code here
            throw new Exception("Something went wrong");
        }
        catch (Exception ex)
        {
            int actionCode = await AlertViewControllerHelper.ShowAlertDialogAsync(ParentViewController, AlertViewControllerHelper.ERRORDESCRIPTION);

            if (actionCode == AlertViewControllerHelper.INFOACTIONCODE)
            {
                await AlertViewControllerHelper.ShowAlertDialogAsync(ParentViewController, string.Format("{0} : {1}", ex.Message, ex.StackTrace), actionCode);
            }
        }   
 }  
  ........
}
  1. Then the Helper method will be implemented like this:

    public static Task<int> ShowAlertDialogAsync(UIViewController parent, string stackTrace, int actionCode = 0)
        {
            bool isDebug = false;
    
    // #if DEBUG
            isDebug = true;
    //#endif
    
           var taskCompletionSource = new TaskCompletionSource<int>();
    
            var alert = UIAlertController.Create(ERROR, stackTrace, UIAlertControllerStyle.Alert);
            if (alert.PopoverPresentationController != null)
            {
                alert.PopoverPresentationController.SourceView = parent.View;
                alert.PopoverPresentationController.SourceRect = parent.View.Bounds;
            }
    
            alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default,
                a => taskCompletionSource.SetResult(0)));
            if (isDebug && actionCode == OKACTIONCODE)
            {
                alert.AddAction(UIAlertAction.Create("Info", UIAlertActionStyle.Default,
                    a => taskCompletionSource.SetResult(1)));
            }
    
            parent.PresentViewController(alert, true, null);
    
            return taskCompletionSource.Task;
        }
    
like image 166
Milen Avatar answered May 17 '26 10:05

Milen