1. Technology

iOS Development in C# with Xamarin Studio and Visual Studio

A quick overview


iPhone Tutorial 2 Screenshot

This is the second in a series of iPhone tutorials on programming iPhone and iPad Apps in C#. You might want to start with the first tutorial.

Note to follow these tutorials you will need a Mac computer and Xamarin Studio. The good news is that Xamarin provides a free version of Xamarin Studio though you are limited to small Apps. You should install Xcode on your Mac (It's free from the Mac Store).

In this tutorial, I'll explain View Controllers and Views and we'll create the first App. This will be nibless. A nib is an XML file that defines the User Interface, a bit like XAML in WPF. In Xamarin C# (and Objective-C) you can use Nibs or develop the App entirely in code which is a bit more powerful.

In Xamarin Studio create an empty project. (File then New Solution) to open the New Solution dialog. Select iOS on the left hand menu then iPhone and select Empty Project. You should now have a project containing:

  • References
  • Components
  • Empty Resources Folder
  • AppDelegate.cs
  • info.plist
  • Main.cs

If you do this from Visual Studio, it will create the same files and attempt to connect to a networked Mac that it uses as the Build Host. If you have been wondering how to get them talking, this is how. It's important for example that they are kept in sync and when say the Mac version is updated, the Visual Studio will update itself.

What is a View Controller?

The main GUI of iOS Apps is displayed in one window and is built out of one or more screens. Each different screen has to have a View Controller.

To launch a new Screen, you instantiate the View Controller. As objects go they are meant to be light weight and start quickly so never do any heavy initialization in a View Controller.

  • Windows - the full display.
  • Screen. The GUI for one purpose say login, print, enter name/address etc.
  • View Controller. An object that manages Views and/or other View Controllers for a screen.
  • View. An object that displays part or all of a screen.
  • Control. Aka SubView. A GUI control like a button, text edit, label etc.

The screen interface is called a View and the controls that make up that view are called subviews (or controls). One view can cover the entire screen or a small part of it and you might have multiple views. It all depends on how you design your GUI.

There are two ways of designing GUIs. One is with nibs (designed in the Xcode Interface Builder) or else nibless, purely in C# code. For now we'll the latter route.

Different View Controllers

Out of the box, iOS provides these View Controllers. All are subclasses (descended) from the UIViewController class. You can also create your own.

  • UINavigationController - Manages Multiple screens (View Controllers)
  • UITabBarController - Manages multiple View Controllers via Tabs
  • UISplitViewController (iPad Only) - a master-detail view.
  • UIPageViewController - Navigate between View Controllers using Page curl/Page view transitions
  • UICollectionViewController - Manage a collection of views
  • UITableViewController - Manmage a table
  • UIImagePickerController - manage image selection.

There are many different view types available from simple controls such as buttons, sliders, to views for showing progress, loading documents, displaying interactive maps etc. Everything that can be done in Xcode with Objective-C can be done with Xamarin.

Views can only be assigned to one controller but a controller can manage many views. The calculator in the iPhone has two different screens. There's normal simple calculator when it's in the normal phone orientation. But rotate it 90 degrees and you get a scientific calculator. That could be done with one controller managing both those views.

So in our first example, We'll add a button, which when clicked pops p a message. All in C# code with nary a nib in sight.

The AppDelegate

All projects have one of these. It contains the code that's called by iOS and creates a Window. The AppDelegate is where we add a View Controller (the first one is called the Root View Controller).

If the iOS has changed something, say your App has to be suspended when a call arrives, it's done via the AppDelegate. For instance it can be switched from running in the foreground to be running in the background. As it may get terminated if the iOS needs more memory, when it enters background you should save important data.

We'll cover these in future tutorials, but for now, let's create our Root View Controller. Xamarin Studio lets you do right click Add in the Solution and you can then pick a controller. I just picked UIViewController, named it to CustomViewController and it added the .cs file and a designer file and a nib. As we're running nibless for this, just remove the other two files leaving CustomViewController.cs.

In the AppDelegate.cs we declare the controller

UIViewController controller;

Just under the UIWindow window declaration and then in the FinishedLaunching override, we create an instance of the controller and set it as the RootViewController.

controller = new CustomViewController();
window.RootViewController = controller;

Now we need to create at least one view, If we have our own view types then we'd stick them in another file, but in this case we'll use the default View that the ViewControiller provides. We'll set the Views background colot to gray with this:

View.BackgroundColor = UIColor.Gray;

Then add a button in code, set the text of that button and have it do something when it's clicked. After we have created the button and set it's properties, the last thing remains to add it to the View.

testButton = UIButton.FromType(UIButtonType.RoundedRect) ;
testButton.Frame = new RectangleF(60, 90, View.Bounds.Width - 120, 44) ;
testButton.SetTitle("Click Me",UIControlState.Normal) ;
testButton.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
testButton.TouchUpInside += delegate
                var alert = new UIAlertView()
                                 Title = "You Clicked Me!",
                                 Message = "I hope you enjoyed that!"
                alert.AddButton("OK") ;

                alert.Show() ;

View.AddSubview(testButton) ;

Once done, try compiling. Make sure in either that you are building Debug and select iPhone Simulator (iPad is equally ok). Then you can try running it.

In the next tutorial, I'll look at some of the other View Controllers and create a more complicated App.

In the next tutorial, we'll look at other View Controllers and develop another App.

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C Programming
  5. iPhone Coding Tutorials
  6. iOS Development in C# Tutorial Two

©2014 About.com. All rights reserved.