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
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
- using System;
- namespace XamarinIosDataPass {
- public class CommonClass {
- public static string value;
- }
- }
Step 14
In this step, go to ViewController.cs page. Write the code given below.
ViewController.cs
- using System;
- using UIKit;
- namespace XamarinIosDataPass {
- public partial class ViewController: UIViewController {
- protected ViewController(IntPtr handle): base(handle) {
-
- }
- public override void ViewDidLoad() {
- base.ViewDidLoad();
-
- }
- partial void BtnSend_TouchUpInside(UIButton sender) {
- CommonClass.value = txtInput.Text;
- }
- public override void DidReceiveMemoryWarning() {
- base.DidReceiveMemoryWarning();
-
- }
- }
- }
Step 15
In this step, go to SecondViewController.cs page. Write the code given below.
SecondViewController.cs
- using Foundation;
- using System;
- using UIKit;
- namespace XamarinIosDataPass {
- public partial class SecondViewController: UIViewController {
- public SecondViewController(IntPtr handle): base(handle) {}
- public override void ViewDidLoad() {
- base.ViewDidLoad();
- lblDisplay.Text = CommonClass.value;
- }
- }
- }
Summary
This was the process of how to pass the data between two ViewController in Xamarin iOS, using Xamarin Studio.