UIPickerView In Xamarin iOS

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.
Prerequisites
  • Xamarin Studio.
  • Xcode.
The steps given below are required to be followed in order to create a Picker View using UIPickerView in Xamarin iOS.

Step 1

Go To Xamarin Studio.

Click New Solution—> select iOS—>select App--> choose Single View App. Afterward, click "Next".

 

Step 2

In this step, configure your app. Give the App Name (Ex:sample), Organization Identifier. Then, click Next.

 

Step 3

In this step, give your project name (Ex: Sample) and solution name (Ex: Sample). Give the path of your project. Afterward, click Create.



Step 4

Subsequently, go to the solution. In the solution, you get all the files and sources available in your project. Now, select Main.storyboard and double click to open Main.storyboard page.

After opening the Main.storyboard, you can design this page as per your desire.

 

Step 5

In this step, design your app using Storyboard, Toolbox, and Properties.
  • UIPicker(CountryPicker).
  • Lable(lblValue).
 

Step 6

Create CountryPickerModel Inheritance class from UIPickerViewModel.

 

Step 7

Go to the CountryPickerModel.cs page and write the code given below in it.

CountryPickerModel.cs
  1. using UIKit;  
  2. using System.Collections.Generic;  
  3. using System;  
  4. namespace XamariniOSUIPicker {  
  5.     public class CountyPickerModel: UIPickerViewModel {  
  6.         List < string > Country;  
  7.         public EventHandler ValueChanged;  
  8.         public string SelectedValue;  
  9.         public CountyPickerModel(List < string > Country) {  
  10.             this.Country = Country;  
  11.         }  
  12.         public override nint GetRowsInComponent(UIPickerView pickerView, nint component) {  
  13.             return Country.Count;  
  14.         }  
  15.         public override nint GetComponentCount(UIPickerView pickerView) {  
  16.             return 1;  
  17.         }  
  18.         public override string GetTitle(UIPickerView pickerView, nint row, nint component) {  
  19.             return Country[(int) row];  
  20.         }  
  21.         public override void Selected(UIPickerView pickerView, nint row, nint component) {  
  22.             var country = Country[(int) row];  
  23.             SelectedValue = country;  
  24.             ValueChanged ? .Invoke(nullnull);  
  25.         }  
  26.     }  
  27. }  
 

Step 8

In this step, go to the ViewController.cs page. Then, write the following code in that file.

ViewController.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using UIKit;  
  4. namespace XamariniOSUIPicker {  
  5.     public partial class ViewController: UIViewController {  
  6.         protected ViewController(IntPtr handle): base(handle) {  
  7.             // Note: this .ctor should not contain any initialization logic.  
  8.         }  
  9.         List < string > Country = new List < string > {  
  10.             "America",  
  11.             "Australia",  
  12.             "Bangladesh",  
  13.             "Canada",  
  14.             "Colombia",  
  15.             "China",  
  16.             "Denmark",  
  17.             "India"  
  18.         };  
  19.         public override void ViewDidLoad() {  
  20.             base.ViewDidLoad();  
  21.             // Perform any additional setup after loading the view, typically from a nib.  
  22.             var picker = new CountyPickerModel(Country);  
  23.             CountryPicker.Model = picker;  
  24.             picker.ValueChanged += (sender, e) => {  
  25.                 lblValue.Text = picker.SelectedValue;  
  26.             };  
  27.         }  
  28.         public override void DidReceiveMemoryWarning() {  
  29.             base.DidReceiveMemoryWarning();  
  30.             // Release any cached data, images, etc that aren't in use.  
  31.         }  
  32.     }  
  33. }  
 

Step 9

Now, go to Run option. Choose Debug and a model from the list of iPhone and iPad simulators.

 

Output

After a few seconds, the app will start running on your iPhone simulator. You will see your app working successfully.

 

You can see the UiPickerView is working successfully.

Summary

This was the process of creating a Picker View using UIPickerView in Xamarin iOS. Please share your comments and feedback.


Similar Articles