Xamarin iOS Alert

Introduction

In this article, we will create a Xamarin iOS application. We will learn how to use UIAlert Controller in order to give messages to the user and ask what operations the user wants to proceed with. This application will contain three buttons, clicking on which will open an alert, which may be a simple alert, an alert with actions, or an alert action sheet.

Prerequisites

  1. Programming in C#.
  2. Basics of Xamarin.

Implementation.

Open Visual Studio Community edition. 

Xamarin

Select single view app and click Next.

Xamarin

Give application name and click Next.

Xamarin

Give a project name and create the application.

Open main.storyboard.

Add three buttons (“Alert”, “Alert(2 actions)”, “Actionsheet”).

Xamarin

Add two labels and write the text respectively.

Xamarin

Your View Controller will look like this.

Xamarin

Double click on all three buttons and add the events corresponding to it.

When you double-click, this will ask where to add the events.

Xamarin

Something like the above template will ask you to where to place the event for the button event.

Your viewController.cs fill will be as follows.

  1. using System;  
  2. using UIKit;  
  3.   
  4. namespace AlertApplication  
  5. {  
  6.     public partial class ViewController : UIViewController  
  7.     {  
  8.         protected ViewController(IntPtr handle) : base(handle)  
  9.         {  
  10.             // Note: this .ctor should not contain any initialization logic.  
  11.         }  
  12.   
  13.         public override void ViewDidLoad()  
  14.         {  
  15.             base.ViewDidLoad();  
  16.             // Perform any additional setup after loading the view, typically from a nib.  
  17.         }  
  18.   
  19.         public override void DidReceiveMemoryWarning()  
  20.         {  
  21.             base.DidReceiveMemoryWarning();  
  22.             // Release any cached data, images, etc that aren't in use.  
  23.         }  
  24.   
  25.         partial void UIButton3_TouchUpInside(UIButton sender)  
  26.         {  
  27.             var alert = UIAlertController.Create("Alert""Backgroung is white!", UIAlertControllerStyle.Alert);  
  28.             ShowViewController(alert,null);  
  29.         }  
  30.   
  31.         partial void UIButton4_TouchUpInside(UIButton sender)  
  32.         {  
  33.             var alert = UIAlertController.Create("Alert""Backgroung is white!", UIAlertControllerStyle.Alert);  
  34.             alert.AddAction(UIAlertAction.Create("No", UIAlertActionStyle.Default, (UIAlertAction obj) =>  
  35.             {  
  36.                 MyLabel.Text = "Choice : No";  
  37.             }));  
  38.             alert.AddAction(UIAlertAction.Create("Yes", UIAlertActionStyle.Default, (UIAlertAction obj) =>  
  39.             {  
  40.                 MyLabel.Text = "Choice : Yes";  
  41.             }));  
  42.             ShowViewController(alert, null);  
  43.         }  
  44.   
  45.         partial void UIButton5_TouchUpInside(UIButton sender)  
  46.         {  
  47.             var alert = UIAlertController.Create("Alert""Backgroung is white!", UIAlertControllerStyle.ActionSheet);  
  48.             alert.AddAction(UIAlertAction.Create("Yes", UIAlertActionStyle.Default, (UIAlertAction obj) =>  
  49.             {  
  50.                 MyLabel.Text = "Choice : No";  
  51.             }));  
  52.             alert.AddAction(UIAlertAction.Create("No", UIAlertActionStyle.Default, (UIAlertAction obj) =>  
  53.             {  
  54.                 MyLabel.Text = "Choice : Yes";  
  55.             }));  
  56.             alert.AddAction(UIAlertAction.Create("May be", UIAlertActionStyle.Default, (UIAlertAction obj) =>  
  57.             {  
  58.                 MyLabel.Text = "Choice : May be";  
  59.             }));  
  60.             ShowViewController(alert, null);  
  61.         }  
  62.     }  

In the above code, you may have different event names (for eg.  UIButton3_TouchUpInside is my event name); maybe you will have a different name. So, make sure that you are writing the events for the corresponding buttons.

Run the application.

You will have the below output.

Xamarin

Click on Simple Alert.

Xamarin

The above is the simple alert displayed.

Click on Alert(2 actions).

Xamarin

Click on Yes button and the “yes” event will execute and label changes its text shows what you have selected.

Xamarin

Same as if you select the “No” option, it will execute event for “no”.

Xamarin

Now click the button “ActionSheet”.

Xamarin

You will have a sheet of options. Clicking on individual options on the sheet will execute the event in the same way as the Button with 2actions does.

Suppose, we click on “Maybe”; the label will change its text with respective event.

Xamarin

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now