Getting started with iOS dev using #Xamarin Part 1

.NET developers wishing to develop for iOS should look at Xamarin Studio. It offers an experience similar to that offered by Visual Studio from the comfort of OS X. If you are a Visual Studio fanatic, Xamarin offers a package with deep integration with Visual Studio that allows you to develop using Visual Studio on Windows (you still need a mac somewhere to be able to compile). Over the last month, I have primarily used Xamarin Studio on MacBook Pro. This is what it looks like on launch

Screen Shot 2015-04-01 at 10.19.21

Just like Visual Studio, Xamarin Studio offers many stock templates. Here is what iOS Unified API templates look like.

Screen Shot 2015-04-01 at 10.21.43

We will start with Single View Application targeting iPhone. Once we add a name, the template creates the project with required files.. main, AppDelegate, Storyboard and the ViewController. The arrow as shown in storyboard indicates the ViewController that is launched.

Screen Shot 2015-04-01 at 10.26.13

There is little you can do with a single View and for that reason let us add NavigationController and set the XiOSDemoViewController as the root view controller (the process is very visual and click oriented and Xamarin Studio makes it less so than Xcode)

Screen Shot 2015-04-01 at 10.37.00

Click and Drag (while holding Ctrl key) from Navigation Controller to the ViewController and set relationship as Root.As you can see we have set the App to delegate control to Navigation Controller which now has the XiOSViewController as the Root controller. Lets drop a few controls.. Navigation Item for Page Title, a label and a couple of buttons. A few things to remember is that if you want set anything during view lifecycle, the view controller provides a few handy methods that can be overridden. Screen Shot 2015-04-01 at 11.03.45 Let’s wire up button’s TouchUpInside Event. This is equivalent Click / Tap event in Windows world. We will wire it up in the view’s load event as shown below

int clickCount = 0;

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();

    // Perform any additional setup after loading the view, typically from a nib.
    this.btnClickMe.TouchUpInside += (sender, e) => 
    {
        clickCount++;
        this.lblClickMessage.Text = String.Format("You clicked {0} times", clickCount);
    };
}

Running the project in simulator and clicking the button increments the count and displays the message. Note that you did not have to associate the controls with ViewControllers.. that was done for you as you would have expected with Visual Studio. Xcode developers have to do lot of click drag from storyboard on to ViewControllers to make them available. This might seem trivial but it is very useful and a no-brainer from IDE perspective.

Screen Shot 2015-04-01 at 11.30.00

Finally lets add a button to the view and a view controller to storyboard. Another nice feature unlike Xcode is that when you set the class name for ViewController in storyboard, Xamarin Studio creates the class for you. You don’t need to create one.. similarly any controls you add are automatically available as i mentioned before. Screen Shot 2015-04-01 at 11.36.51 Screen Shot 2015-04-01 at 11.38.21 To navigate from one view to another, you can add a segue from button to view controller or you can do so in code. I have added a visual segue and chose action as push. Thats it.. now when you run the app navigation is in place.

One thought on “Getting started with iOS dev using #Xamarin Part 1

  1. Pingback: Getting started with iOS dev using #Xamarin Part 1 | Dinesh Ram Kali.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s