How To Pass Data Between Two ViewControllers In Xamarin iOS

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android and 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 pass the data between two ViewControllers in Xamarin iOS, using Xamarin Studio.

Step 1

Go to Xamarin Studio.

Click New Solution—> select iOS—>select App--> Choose single View App. Afterwards, click Next.

 

Step 2

In this step, configure your app. Give the app name (Ex:sample), Organization Identifier. Afterwards, click Next.



Step 3

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


Step 4

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

 

Step 5

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

 

Step 6

Now, go to the toolbox Window. In the toolbox Window, get all the types of the tools and controls. You need to go to the toolbox Window. Now, scroll down and you will see all the tools and controls. You need to drag and drop View Controller and create class for SecondViewController.

 

Step 7

You need to drag and drop the Text Field.

Now, you need to align the Text Field, using constraints and go to Properties Window. Select Text Field and give a name (Ex:txtInput).

 

Step 8

You need to drag and drop the button.

Now, you need to align the button, using the constraints and go to Properties Window. Select the button and give a name (Ex:btnSend).


Step 9

You need to drag and drop the Label in Second ViewController.

Now, you need to align the Label, using the constraints and go to properties Window. Select the Label and give a name (Ex:lblDisplay).

 

Step 10

In this step, show the second view controller. You can click the button (Ctrl +Right Click+ Move). Move the cursor to Second View Controller. If it released, the click shows the list of the options and you can choose Show.

 

Step 11

In this step, add one class file. Go to Solution Explorer—>Right Click—>Add—>New File.

 

Step 12

Now, choose Empty Class file and give the name (Ex:commonclass.cs) to create New file.

 

Step 13

In this step, go to the CommonClass.cs page. Write the code given below.

CommonClass.cs
  1. using System;  
  2. namespace XamarinIosDataPass {  
  3.     public class CommonClass {  
  4.         public static string value;  
  5.     }  
  6. }  


Step 14

In this step, go to ViewController.cs page. Write the code given below.

ViewController.cs
  1. using System;  
  2. using UIKit;  
  3. namespace XamarinIosDataPass {  
  4.     public partial class ViewController: UIViewController {  
  5.         protected ViewController(IntPtr handle): base(handle) {  
  6.             // Note: this .ctor should not contain any initialization logic.  
  7.         }  
  8.         public override void ViewDidLoad() {  
  9.             base.ViewDidLoad();  
  10.             // Perform any additional setup after loading the view, typically from a nib.  
  11.         }  
  12.         partial void BtnSend_TouchUpInside(UIButton sender) {  
  13.             CommonClass.value = txtInput.Text;  
  14.         }  
  15.         public override void DidReceiveMemoryWarning() {  
  16.             base.DidReceiveMemoryWarning();  
  17.             // Release any cached data, images, etc that aren't in use.  
  18.         }  
  19.     }  
  20. }  
 

Step 15

In this step, go to SecondViewController.cs page. Write the code given below.

SecondViewController.cs
  1. using Foundation;  
  2. using System;  
  3. using UIKit;  
  4. namespace XamarinIosDataPass {  
  5.     public partial class SecondViewController: UIViewController {  
  6.         public SecondViewController(IntPtr handle): base(handle) {}  
  7.         public override void ViewDidLoad() {  
  8.             base.ViewDidLoad();  
  9.             lblDisplay.Text = CommonClass.value;  
  10.         }  
  11.     }  
 

Step 16

Now, go to Run option. Choose Debug and the list of iPhone and iPad Simulators, which are available. You can choose any one Simulator and run it.

 

Output

After a few seconds, the app will start running on your iPhone Simulator. You will see your app is working successfully. You can send any data via text field. It will be sent successfully.

 

Summary

This was the process of how to pass the data between two ViewController in Xamarin iOS, using Xamarin Studio.