Follow me on Twitter at Twitter.com/wbm
FYI, I'm blogging most of my stuff over at More Wally now.
You might want to add my rss feed to your reader at:http://morewally.com/cs/blogs/wallym/rss.aspx
UIPicker in the iPhone with MonoTouch - Wallace B. McClure

Wallace B. McClure

All About Wally McClure - The musings of Wallym on Web, HTML5, Mobile, Xamarin.iOS, Xamarin.Android, and Windows Azure.

News

Visual Studio Magazine Column Personal Blog

.NET

Book Authors

Business

Family

Friends

Georgia Tech Bloggers

Personal

Archives

UIPicker in the iPhone with MonoTouch

 UIPicker

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

Comments

Jim Wilson said:

Well done--worked great! Nice eBook by the way. I hope there's another edition that adds stuff like this in it.

# January 12, 2010 2:03 PM