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
- Programming in C#.
- Basics of Xamarin.
Implementation.
Open Visual Studio Community edition.
Select single view app and click Next.
Give application name and click Next.
Give a project name and create the application.
Open main.storyboard.
Add three buttons (“Alert”, “Alert(2 actions)”, “Actionsheet”).
Add two labels and write the text respectively.
Your View Controller will look like this.
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.
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.
- using System;
- using UIKit;
-
- namespace AlertApplication
- {
- public partial class ViewController : UIViewController
- {
- protected ViewController(IntPtr handle) : base(handle)
- {
-
- }
-
- public override void ViewDidLoad()
- {
- base.ViewDidLoad();
-
- }
-
- public override void DidReceiveMemoryWarning()
- {
- base.DidReceiveMemoryWarning();
-
- }
-
- partial void UIButton3_TouchUpInside(UIButton sender)
- {
- var alert = UIAlertController.Create("Alert", "Backgroung is white!", UIAlertControllerStyle.Alert);
- ShowViewController(alert,null);
- }
-
- partial void UIButton4_TouchUpInside(UIButton sender)
- {
- var alert = UIAlertController.Create("Alert", "Backgroung is white!", UIAlertControllerStyle.Alert);
- alert.AddAction(UIAlertAction.Create("No", UIAlertActionStyle.Default, (UIAlertAction obj) =>
- {
- MyLabel.Text = "Choice : No";
- }));
- alert.AddAction(UIAlertAction.Create("Yes", UIAlertActionStyle.Default, (UIAlertAction obj) =>
- {
- MyLabel.Text = "Choice : Yes";
- }));
- ShowViewController(alert, null);
- }
-
- partial void UIButton5_TouchUpInside(UIButton sender)
- {
- var alert = UIAlertController.Create("Alert", "Backgroung is white!", UIAlertControllerStyle.ActionSheet);
- alert.AddAction(UIAlertAction.Create("Yes", UIAlertActionStyle.Default, (UIAlertAction obj) =>
- {
- MyLabel.Text = "Choice : No";
- }));
- alert.AddAction(UIAlertAction.Create("No", UIAlertActionStyle.Default, (UIAlertAction obj) =>
- {
- MyLabel.Text = "Choice : Yes";
- }));
- alert.AddAction(UIAlertAction.Create("May be", UIAlertActionStyle.Default, (UIAlertAction obj) =>
- {
- MyLabel.Text = "Choice : May be";
- }));
- ShowViewController(alert, null);
- }
- }
- }
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.
Click on Simple Alert.
The above is the simple alert displayed.
Click on Alert(2 actions).
Click on Yes button and the “yes” event will execute and label changes its text shows what you have selected.
Same as if you select the “No” option, it will execute event for “no”.
Now click the button “ActionSheet”.
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.