UIPicker in the iPhone with MonoTouch
The UIPicker is visually different than the drop down listbox that most .NET developers are familiar, however, it is designed to perform the same type of function. It allows users to select from a fixed set of data isntead of typing in data in a text box. Programming with it is fairly simple. Inherit from the UIPickerViewModel class and then bind the data.
Here's the class:
using System;
using MonoTouch;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
namespace OpenUrl
{
public class ProtocolData : UIPickerViewModel
{
public static string[] protocols = new string[]
{
"http://", "tel:","http://maps.google.com/maps?q=", "sms:",
"mailto:"
};
public string[] protocolNames = new string[]
{
"Web", "Phone Call", "Google Maps", "SMS", "Email"
};
AppDelegate ad;
public ProtocolData(AppDelegate pad){
ad = pad;
}
public override int GetComponentCount(UIPickerView uipv)
{
return(1);
}
public override int GetRowsInComponent( UIPickerView uipv, int comp)
{
//each component has its own count.
int rows = protocols.Length;
return(rows);
}
public override string GetTitle(UIPickerView uipv, int row, int comp)
{
//each component would get its own title.
string output = protocolNames[row];
return(output);
}
public override void Selected(UIPickerView uipv, int row, int comp)
{
ad.SelectedRow = row;
}
public override float GetComponentWidth(UIPickerView uipv, int comp){
return(300f);
}
public override float GetRowHeight(UIPickerView uipv, int comp){
return(40f);
}
}
}
And then you bind data doing something like this:
ProtocolData protocolDataSource = new ProtocolData(this);
ProtocolSelection.Model = protocolDataSource;
And there you go, you now have a UIPicker with a list of data.
Want to know more about developing with the iPhone? Check out my Wrox Blox eBook on developing applications with MonoTouch for the iPhone/iPod touch for .NET/C# developers.