When creating applications for iOS, it is important to manage various views or windows, which allow our application to be distributed at different levels of operation. In this example, we will make an application with .NET for iOS and XCode Interface Builder that allows the recording of data of a file in XML format, and that can be consulted in another view.
To do so, it is essential to have the knowledge of creating an application for .NET for iOS, as I explain in the following article, which I recommend observing before making the current one:
Step 1
We create a .NET for iOS application in Visual Studio for Mac and add three new ViewController files called ExtractViewController, PrincipalViewController, and RegisterViewController, removing the XIBs files since we will be using a Storyboard, which we will add to the project with the name Main.
Step 2
We added 3 images, which will serve as the background for each of the 3 views of the application.
Step 3
We open in Visual Studio for Mac, our Main.Storyboard with the XCode Interface Builder, and add 3 View Controllers.
Step 4
We assign the class PrincipalView to the first view and set it as Home View.
Step 5
We add an ImageView that occupies the entire view
Step 6
In the image attributes, in the Image property, we select Image1.jpg, and its Content Mode is set to Scale to Fill.
Step 7
We add two buttons, Register and Extract to the view. From there, we select with the right mouse button the Triggered Segues called action, we take it and drag it to the second view, from the floating menu we select Present Modally, generating the connection line, and once selected in its attributes, in the Transition property we select Cover Vertical.
Step 8
We do the same process for the other button, now pointing to the third view. In the end, we select the Flip Horizontal value as Transition.
Step 9
In the second view, we proceed to connect your Custom Class with the RegisterViewController.
Step 10
We add an ImageView that covers the entire screen and select Image2.jpg.
Step 11
In the third view, we proceed to connect your Custom Class with ExtractViewController.
Step 12
We add an ImageView that covers the entire screen and select Image3.jpg.
Step 13
In views two and three, we added a button with the text Return to Main.
Step 14
We do the same process, but now in the opposite direction, the Return to the Main button will take us to the main view.
Step 15
Now we proceed to add the necessary TextField in view two corresponding to the fields we want to capture.
Step 16
We add their placeholder, and we are placing them on the screen:
Step 17
We add a button and assign the properties we want to it.
Step 18
We do the same process for view 3.
Step 19
Now that we have the elements of the Graphic Interface ready, we proceed to make the link with the names that each of the objects on the screen will have.
Step 20
In view two, let's ensure we're doing the Referencing Outlets with the RegisterViewController.h file.
Step 21
Each element must have its corresponding variable and name. (Note: Do not forget to consult the article Create Apps for iOS with .NET 7 in case you do not remember how to make the bindings)
Step 22
We do the same process in view 3, which is the ExtractViewController.
Step 23
In Visual Studio for Mac, we proceed to prepare the main window, with the setting of the PublicPrincipalView(IntPrt handle):base(handle).
Step 24
We comment the code from AppDelegate.cs, let's not forget to also add Main as the starting Storyboard in the Info.plist.
Step 25
We confirm that the RegisterViewController.designer.cs contains the visual objects we made in the XCode.
Step 26
We do the same process in the ExtractViewController.
Step 27
We run the application to confirm the operation of our graphical interface.
Step 28
We generate a class called Info.cs, where we add the fields that we want to save in our XML file.
using System;
namespace iOSNETVariasVistasXML
{
public class Info
{
public int ID;
public string Name;
public string Address;
public string Mail;
public int Age;
public double Balance;
}
}
Step 29
We add the System.Xml.Serialization library that we will use in the RegisterViewController. Later in the ViewDidLoad, we enable the Touch of the button, and we create the instance of the Info.cs class and we receive the values of the elements of the graphical interface. Later we generate the variable of serialization of the XML file with the structure of the Info.cs class, we prepare the route with a StreamWriter, allowing us to create the file in that format. We proceed to serialize it with the information of the DC variable, having the route and the information that the user entered in the Graphical Interface. We close the serializer and send a message to the screen whose method we will create below, and clear the text boxes.
using System.Xml.Serialization;
namespace iOSNETVariasVistasXML;
public partial class RegisterViewController : UIViewController
{
public RegisterViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
btnSave.TouchUpInside += delegate
{
var DC = new Info();
try
{
DC.ID = int.Parse(txtID.Text);
DC.Name = txtName.Text;
DC.Address = txtAddress.Text;
DC.Mail = txtMail.Text;
DC.Age = int.Parse(txtAge.Text);
DC.Balance = double.Parse(txtBalance.Text);
var WriteXML = new XmlSerializer(typeof(Info));
var serializer = new StreamWriter
(Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal),
txtID.Text + ".xml"));
WriteXML.Serialize(serializer, DC);
serializer.Close();
MessageBox("Save on iPhone", "File XML");
txtID.Text = "";
txtName.Text = "";
txtAddress.Text = "";
txtMail.Text = "";
txtAge.Text = "";
txtBalance.Text = "";
}
catch (Exception ex)
{
MessageBox("Error: ", ex.Message);
}
};
}
}
Step 30
We create the method for the message on the screen using the iOS AlertController.
public static void MessageBox(string Title, string Message)
{
var Alerta = UIAlertController.Create(Title, Message,
UIAlertControllerStyle.Alert);
Alerta.AddAction(UIAlertAction.Create("Done",
UIAlertActionStyle.Default, null));
var Instancia = UIApplication.SharedApplication.Windows[1].
RootViewController;
Instancia.PresentViewController(Alerta, true, null);
}
Step 31
In the ExtractViewController file, we add the same System.Xml.Serialization library, enable the Touch of the button and create the instance of the Info.cs class, we enable the read variable with the serializer and a StreamReader and generate the path where the file is stored. We read the file through the deserialization of the XmlSerializer, extract the information from each variable, and deposit it in the user's text boxes. Finally, we add the Messabox method with the AlertController.
using System.Xml.Serialization;
namespace iOSNETVariasVistasXML;
public partial class ExtractViewController : UIViewController
{
public ExtractViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
btnExtract.TouchUpInside += delegate
{
var DC = new Info();
try
{
var Lectura = new XmlSerializer(typeof(Info));
var serializador = new StreamReader
(Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal),
txtID.Text + ".xml"));
var info = (Info)Lectura.Deserialize
(serializador);
serializador.Close();
txtName.Text = info.Name;
txtAddress.Text = info.Address;
txtMail.Text = info.Mail;
txtAge.Text = info.Age.ToString();
txtBalance.Text = info.Balance.ToString();
}
catch (Exception ex)
{
MessageBox("Error: ", ex.Message);
}
};
}
}
Complete Code
using System;
namespace iOSNETVariasVistasXML
{
public class Info
{
public int ID;
public string Name;
public string Address;
public string Mail;
public int Age;
public double Balance;
}
}
using System.Xml.Serialization;
namespace iOSNETVariasVistasXML;
public partial class RegisterViewController : UIViewController
{
public RegisterViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
btnSave.TouchUpInside += delegate
{
var DC = new Info();
try
{
DC.ID = int.Parse(txtID.Text);
DC.Name = txtName.Text;
DC.Address = txtAddress.Text;
DC.Mail = txtMail.Text;
DC.Age = int.Parse(txtAge.Text);
DC.Balance = double.Parse(txtBalance.Text);
var WriteXML = new XmlSerializer(typeof(Info));
var serializer = new StreamWriter
(Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal),
txtID.Text + ".xml"));
WriteXML.Serialize(serializer, DC);
serializer.Close();
MessageBox("Save on iPhone", "File XML");
txtID.Text = "";
txtName.Text = "";
txtAddress.Text = "";
txtMail.Text = "";
txtAge.Text = "";
txtBalance.Text = "";
}
catch (Exception ex)
{
MessageBox("Error: ", ex.Message);
}
};
}
public static void MessageBox(string Title, string Message)
{
var Alerta = UIAlertController.Create(Title, Message,
UIAlertControllerStyle.Alert);
Alerta.AddAction(UIAlertAction.Create("Done",
UIAlertActionStyle.Default, null));
var Instancia = UIApplication.SharedApplication.Windows[1].
RootViewController;
Instancia.PresentViewController(Alerta, true, null);
}
}
using System.Xml.Serialization;
namespace iOSNETVariasVistasXML;
public partial class ExtractViewController : UIViewController
{
public ExtractViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
btnExtract.TouchUpInside += delegate
{
var DC = new Info();
try
{
var Lectura = new XmlSerializer(typeof(Info));
var serializador = new StreamReader
(Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal),
txtID.Text + ".xml"));
var info = (Info)Lectura.Deserialize
(serializador);
serializador.Close();
txtName.Text = info.Name;
txtAddress.Text = info.Address;
txtMail.Text = info.Mail;
txtAge.Text = info.Age.ToString();
txtBalance.Text = info.Balance.ToString();
}
catch (Exception ex)
{
MessageBox("Error: ", ex.Message);
}
};
}
public static void MessageBox(string Titulo, string Mensaje)
{
var Alerta = UIAlertController.Create(Titulo, Mensaje,
UIAlertControllerStyle.Alert);
Alerta.AddAction(UIAlertAction.Create("OK",
UIAlertActionStyle.Default, null));
var Instancia = UIApplication.SharedApplication.Windows[1].
RootViewController;
Instancia.PresentViewController(Alerta, true, null);
}
}
Step 32
We run and got ready. We have an iPhone application using .NET for iOS that locally stores a record in XML and queries it using different views on our Storyboard.
Thank you very much.
@enriqueaguilar