This is Part 2 of our series building a xib-free iOS app with C#.
- Introduction
- Clock
- Timers
- Add an iAd banner
- Landscape and auto layout
- Get current location
- NuGet
- Get current weather
- Profit!
Create a New Project in Xamarin Studio
Start by creating an iOS solution in Xamarin Studio using the iPhone Empty Project template just like you did last time when following the Xamarin article, Creating iOS Applications in Code. Name the project CurrentWeatherClock.
Xamarin Studio will create a new solution, open it, and open the AppDelegate.cs
file:
[code lang=text] using System; using System.Collections.Generic; using System.Linq; using MonoTouch.Foundation; using MonoTouch.UIKit;
namespace CurrentWeatherClock { // The UIApplicationDelegate for the application. This class is responsible for launching the // User Interface of the application, as well as listening (and optionally responding) to // application events from iOS. [Register (“AppDelegate”)] public partial class AppDelegate : UIApplicationDelegate { // class-level declarations UIWindow window; // // This method is invoked when the application has loaded and is ready to run. In this // method you should instantiate the window, load the UI into it and then make the window // visible. // // You have 17 seconds to return from this method, or iOS will terminate your application. // public override bool FinishedLaunching (UIApplication app, NSDictionary options) { // create a new window instance based on the screen size window = new UIWindow (UIScreen.MainScreen.Bounds);
// If you have defined a root view controller, set it here: // window.RootViewController = myViewController;
// make the window visible window.MakeKeyAndVisible ();
return true; } } } [/code]
If you build and run, you’ll get a black screen and an error message in the console window of Xamarin Studio:
Application windows are expected to have a root view controller at the end of application launch
Create a Custom View Controller
Add a new class named CurrentWeatherClockViewController
via one of the following methods:
- Select menu File > New > File
- Press Command-N
- Right click on the project and select menu option Add > New File…
Edit your new class to add the MonoTouch.UIKit
namespace and inherit from UIViewController
:
[code lang=text] using System;
using MonoTouch.UIKit;
namespace CurrentWeatherClock { public class CurrentWeatherClockViewController : UIViewController { public CurrentWeatherClockViewController () { } } } [/code]
If you build and run at this point, you won’t see anything different because we haven’t hooked this view controller into the main window yet.
Initialize the View
Get rid of the constructor for now and override the ViewDidLoad
method as follows:
[code lang=text] using System;
using MonoTouch.UIKit;
namespace CurrentWeatherClock { public class CurrentWeatherClockViewController : UIViewController { public override void ViewDidLoad() { base.ViewDidLoad(); this.View.BackgroundColor = UIColor.White; } } } [/code]
Before you build and run, we need to create an object of this class and hook it into the main window’s RootViewController
. Open the AppDelegate
class and replace this code:
[code lang=text] // If you have defined a root view controller, set it here: // window.RootViewController = myViewController; [/code]
with this:
[code lang=text] window.RootViewController = new CurrentWeatherClockViewController (); [/code]
Create and Add the Clock View
Add an instance of UILabel class:
[code lang=text] private UILabel timeLabel; [/code]
Create the object and add it to the view controller’s view:
[code lang=text] using System; using System.Drawing;
using MonoTouch.UIKit;
namespace CurrentWeatherClock { public class CurrentWeatherClockViewController : UIViewController { private UILabel timeLabel;
public override void ViewDidLoad() { base.ViewDidLoad(); this.View.BackgroundColor = UIColor.White;
this.timeLabel = new UILabel { Frame = new RectangleF(0, 180, this.View.Bounds.Width, 100), Text = “00:00:00 AM”, Font = UIFont.SystemFontOfSize(56) };
this.View.AddSubview (this.timeLabel); } } } [/code]
Now when you build and run, you will be see your clock label on a white background. Congratulations, you’ve just created a xib-free iOS app in C#!
In the immortal words of Ron Popeil, “But wait, there’s more!”
Next time, we will discuss how to get the clock running and displaying the current time.