My First IPhone App – iTrade

Idea:  Build an app that allows the traders to update their positions while not around a computer.  For example, a trader who is sitting on a train when the Greek riots broke out, should be able to see the news and immediately be able to close out his / her positions.

Design (0.1)

– Login screen
– Display traders positions
– Display news (RSS) feeds
– Allow the trader to update his position
– Use web services for trade communications.

I started with an Ipad application just because further down the road, I would like to add a few more things like charts which require a lot of real estate.

NAVIGATION SCREEN

Navigation Screen has 2 rows – Trade Row and News Feed row.
I started with a standard ipad solution – IPad Windows Based Project. Next in monotouch solution double click on MainWindow.xib to open it in InterfaceBuilder. Drag a NavigationController in “main window”. Expand the “NavigationController” to see the “RootViewController”. We will come to this later. Save your interface builder and in your MonoTouch solution, main.cs would look like this.

#Main.cs

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace iTrade
{
	public class Application
	{
		static void Main (string[] args)
		{
			UIApplication.Main (args);
		}
	}

	// The name AppDelegate is referenced in the MainWindow.xib file.
	public partial class AppDelegate : UIApplicationDelegate
	{
		// This method is invoked when the application has loaded its UI and its ready to run
		public override bool FinishedLaunching (UIApplication app, NSDictionary options)
		{
			ApplicationData.IsLoggedIn = false;
			// If you have defined a view, add it here:
			window.AddSubview (navigationController.View);
			window.MakeKeyAndVisible ();

			return true;
		}

	}
}

RootViewController
In your solution, add a new “Controller with a View” for IPad and name it “RootViewController”. Click your MainWindow.xib again and in the identity window – set the class to “RootViewController” – the one that you just added and save it. Back to your MonoTouch solution, double click “RootViewController.xib” and add a UITableView in InterfaceBuilder. You can set the properties to however you want the initial trade screen to look like. I have my style set to grouped. Next your UITableView needs a TableSource. Add a plain class and call it “RootDataSource”. This class needs to derive from UITableViewSource, so that its object can be a table source for the root table. Here is how the RootDataSource class looks like.

using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace iTrade
{
	enum SECTIONS
	{
		TRADER,
		MAX,

	}
	enum TRADER_ROW
	{
		TRADE,
		NEWS,
		MAX
	}
	enum LOGIN_ROW
	{
		LOGIN,
		MAX
	}

	public class RootDataSource : UITableViewSource
	{
		RootViewController _controller;
		public RootDataSource (RootViewController controller)
		{
			_controller = controller;
		}

		public override int RowsInSection (UITableView tableView, int section)
		{
			switch (section)
			{
			case (int)SECTIONS.TRADER:
				return (int)TRADER_ROW.MAX;
			default:
				return 0;
			}
		}

		public override int NumberOfSections (UITableView tableView)
		{
			Console.Write("ApplicationData.IsLoggedIn: " + (ApplicationData.IsLoggedIn ? "True" : "False"));
			if(ApplicationData.IsLoggedIn)
				 return (int)SECTIONS.MAX -1;
			else
				return (int) SECTIONS.MAX;
		}
		public override string TitleForHeader (UITableView tableView, int section)
		{
			switch(section)
			{
			case (int)SECTIONS.TRADER:
				return "Trader Options";
			default:
				return "Other Options";
			}

		}
		string getCellNameForTraderSection(int row)
		{
			switch(row)
			{
			case (int) TRADER_ROW.TRADE:
				return  "Trade";

			case (int)TRADER_ROW.NEWS:
				return "News";

			default:
				return "";
			}

		}
		public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
		{
			var cell = new UITableViewCell(UITableViewCellStyle.Default, "");
			var _cellTitle = "";
			switch(indexPath.Section)
			{
			case (int)SECTIONS.TRADER:
				_cellTitle = getCellNameForTraderSection(indexPath.Row);
				break;
			default:
				break;
			}//switch(indexPath.Section)
			cell.TextLabel.Text = _cellTitle;

			return cell;
		}

		public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
		{
			if (indexPath.Section == (int)SECTIONS.TRADER)
			{
				switch(indexPath.Row)
				{
				case (int)TRADER_ROW.TRADE:
					{
						_controller.NavigationController.PushViewController(new  PositionAndTradeController(), true);
						break;
					}
				case (int)TRADER_ROW.NEWS:
					{
						_controller.NavigationController.PushViewController(new NewsFeedController(), true);
						break;
					}
				}//switch(indexPath.Row)
			}//SECTION = TRADER

		}

	}
}

Enums are used for simplicity.  The PositionsAndTradeController and the NewFeedController are controllers similar to RootViewController.  When a certain row is selected, the respective controller is pushed on the navigation controller.

The RootViewContoller looks like this

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace iTrade
{
	public partial class RootViewController : UITableViewController
	{
		#region Constructors

		// The IntPtr and initWithCoder constructors are required for controllers that need
		// to be able to be created from a xib rather than from managed code

		public RootViewController (IntPtr handle) : base(handle)
		{
			Initialize ();
		}

		[Export("initWithCoder:")]
		public RootViewController (NSCoder coder) : base(coder)
		{
			Initialize ();
		}

		public RootViewController () : base("RootViewController", null)
		{
			Initialize ();
		}

		void Initialize ()
		{
		}
		public override void ViewDidLoad ()
		{
			Console.WriteLine("RootViewController ViewDidLoad");

			base.ViewDidLoad ();
			this.TableView.Source = new RootDataSource(this);
			this.Title = "iTrade";
			Console.WriteLine("DidLoad End.");
		}

		#endregion

	}
}

An important point to note in the RootViewController, is that the “Source” property of the TableView is used instead of the DataSource

This is a very basic implementation of Navigation controllers.  In the next blog I will show how to implement “login” Controller and present it before the root controller is displayed.

Advertisements
  1. No trackbacks yet.

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

%d bloggers like this: