Skip to content

Instantly share code, notes, and snippets.

@tomaszkubacki
Created October 6, 2012 11:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomaszkubacki/3844649 to your computer and use it in GitHub Desktop.
Save tomaszkubacki/3844649 to your computer and use it in GitHub Desktop.
monotouch keyboard editor and combobox enum editor as UITextField exntesions

ios default keyboard editor does not have close button if you need show editor for numeric value or edit enum val code below may be helpful. Just copy and paste to your project.

copy/paste utils file from second file and then this is all you need to display number pad with close button

myTextField.SetKeyboardEditorWithCloseButton(UIKeyboardType.NumberPad); 

(keyboard editor)keyboard editor

now here is how you can edit enum

enum WhatIsTheBestWebserviceLibForNetAndMono {
	ServiceStack,
	OfCourseServiceStack,
	ForSureServiceStack
}

WhatIsTheBestWebserviceLibForNetAndMono currentSelection;

webserviceSelectorTextField.SetPickerEditor(
	currentSelection,
	(Enum newVal) => {currentSelection = (WhatIsTheBestWebserviceLibForNetAndMono)newVal;},
	"Close"
);

(enum editor)enum editor

this work is based on this blog post: thirteendaysaweek and this repo: MonoTouchUIPickerView

public static class UIExtensions
{
public static void SetKeyboardEditorWithCloseButton (this UITextField txt, UIKeyboardType keyboardType, string closeButtonText = "Done")
{
UIToolbar toolbar = new UIToolbar ();
txt.KeyboardType = keyboardType;
toolbar.BarStyle = UIBarStyle.Black;
toolbar.Translucent = true;
toolbar.SizeToFit ();
UIBarButtonItem doneButton = new UIBarButtonItem (closeButtonText, UIBarButtonItemStyle.Done,
(s, e) => {
txt.ResignFirstResponder ();
});
toolbar.SetItems (new UIBarButtonItem[]{doneButton}, true);
txt.InputAccessoryView = toolbar;
}
public static void SetPickerEditor (this UITextField txt, Enum enm, Action<Enum> valueChanged, string closeButtonText = "Done")
{
List<string> values = new List<string> ();
Type enumType = enm.GetType ();
Dictionary<string,Enum> enumVals = new Dictionary<string, Enum> ();
string selectedValue = Locale.GetText (valueChanged.ToString ());
foreach (var v in Enum.GetValues(enumType)) {
var key = Locale.GetText (v.ToString ());
values.Add (key);
enumVals.Add (key, v as Enum);
}
Action<string> valueChangedHandler = (string v) => {
valueChanged (enumVals [v]);
};
txt.SetPickerEditor (values, selectedValue, valueChangedHandler, closeButtonText);
}
public static void SetPickerEditor (this UITextField txt, IList<string> dataSource, string initValue, Action<string> valueChanged, string closeButtonText = "Done")
{
PickerModel model = new PickerModel (dataSource);
string selectedValue = null;
model.PickerChanged += (sender, e) => {
selectedValue = e.SelectedValue;
valueChanged (selectedValue);
};
UIPickerView picker = new UIPickerView ();
int initValIndex = dataSource.IndexOf (initValue);
if (initValIndex >= 0)
picker.SelectedRowInComponent (initValIndex);
picker.ShowSelectionIndicator = true;
picker.Model = model;
UIToolbar toolbar = new UIToolbar ();
toolbar.BarStyle = UIBarStyle.Black;
toolbar.Translucent = true;
toolbar.SizeToFit ();
UIBarButtonItem doneButton = new UIBarButtonItem (closeButtonText, UIBarButtonItemStyle.Done,
(s, e) => {
txt.Text = selectedValue;
txt.ResignFirstResponder ();
});
toolbar.SetItems (new UIBarButtonItem[]{doneButton}, true);
txt.InputView = picker;
txt.InputAccessoryView = toolbar;
}
}
public class PickerModel : UIPickerViewModel
{
private readonly IList<string> values;
public event EventHandler<PickerChangedEventArgs> PickerChanged;
public PickerModel (IList<string> values)
{
this.values = values;
}
public override int GetComponentCount (UIPickerView picker)
{
return 1;
}
public override int GetRowsInComponent (UIPickerView picker, int component)
{
return values.Count;
}
public override string GetTitle (UIPickerView picker, int row, int component)
{
return values [row];
}
public override float GetRowHeight (UIPickerView picker, int component)
{
return 40f;
}
public override void Selected (UIPickerView picker, int row, int component)
{
if (this.PickerChanged != null) {
this.PickerChanged (this, new PickerChangedEventArgs{SelectedValue = values[row]});
}
}
}
public class PickerChangedEventArgs : EventArgs
{
public string SelectedValue { get; set; }
}
public static class Locale
{
static NSBundle main = NSBundle.MainBundle;
public static string GetText (string str)
{
return main.LocalizedString (str, String.Empty, String.Empty);
}
public static string Format (string fmt, params object [] args)
{
var msg = main.LocalizedString (fmt, String.Empty, String.Empty);
return String.Format (msg, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment