Skip to content

Instantly share code, notes, and snippets.

@MorningZ
Created April 12, 2011 14:04
Show Gist options
  • Save MorningZ/915540 to your computer and use it in GitHub Desktop.
Save MorningZ/915540 to your computer and use it in GitHub Desktop.
Create slide up picker views to give your user a nice way to make choices (for iOS)
/**
* @description: This is the iPhone version to create picker views on the fly :-)
* @author: Stephen G
* @date: 04/11/2011
* @screenshot: http://yfrog.com/h4vr6ep
*
* @thanks to: http://cssgallery.info/making-a-combo-box-in-titanium-appcelerator-code-and-video/
*
* Example of use (two different pickers using same function to create):
*
* var data = [];
* data.push(Titanium.UI.createPickerRow({ title : "Baseball", custom_item : "1" }));
* data.push(Titanium.UI.createPickerRow({ title : "Basketball", custom_item : "2" }));
* data.push(Titanium.UI.createPickerRow({ title : "Football", custom_item : "3" }));
* var PickerSport = Controls.CreatePickerView(
* Titanium.UI.currentWindow,
* data,
* true,
* function(row) {
* Titanium.API.info("You picked: " + row.title + "/" + row.custom_item);
* }
* );
*
* PickerSport.Show(); // Shows the picker view (you call this)
* PickerSport.Hide(); // Hides the picker view (called automatically from buttons inside the view)
*
* data = [];
* data.push(Titanium.UI.createPickerRow({ title : "Boston", custom_item : "1" }));
* data.push(Titanium.UI.createPickerRow({ title : "New York", custom_item : "2" }));
* data.push(Titanium.UI.createPickerRow({ title : "Miami", custom_item : "3" }));
* var PickerLocation = Controls.CreatePickerView(
* Titanium.UI.currentWindow,
* data,
* true,
* function(row) {
* Titanium.API.info("You picked: " + row.title + "/" + row.custom_item);
* }
* );
*
* PickerLocation.Show(); // Shows the picker view (you call this)
* PickerLocation.Hide(); // Hides the picker view (called automatically from buttons inside the view)
*
*/
var Controls = {};
Controls.CreatePickerView = function(wn, data, logEnabled, done) {
/// <summary>Creates a slideup-ing View that holds a picker</summary>
/// <param name="wn" type="Titanium.UI.Window">
/// A reference to the Window to add this PickerView to
/// </param>
/// <param name="data" type="array of Titanium.UI.PickerRow">
/// an array of data rows to add to the picker
/// </param>
/// <param name="logEnabled" type="boolean">
/// Enabled logging inside this function
/// </param>
/// <param name="done" type="Function">
/// The event to run when the user picks "done", function will be passed the Titanium.UI.PickerRow that they chose
/// </param>
/// <returns type="Titanium.UI.View" />
var ThisControl = {
"LogEnabled" : logEnabled,
"Log" : function(txt) {
if (ThisControl.LogEnabled) { Titanium.API.info(txt); }
}
};
ThisControl.Log("Inside 'Controls.CreatePickerView' - Start");
// The container view, which will be hidden along the bottom
ThisControl.View = Titanium.UI.createView({
height : 251,
bottom : -251
});
// Show the view holding the picker
ThisControl.ShowAnimation = Titanium.UI.createAnimation({bottom:0});
ThisControl.Show = function() {
ThisControl.View.animate(ThisControl.ShowAnimation);
};
// Hide the view holding the picker
ThisControl.HideAnimation = Titanium.UI.createAnimation({bottom:-251});
ThisControl.Hide = function() {
ThisControl.View.animate(ThisControl.HideAnimation);
};
ThisControl.Cancel = Titanium.UI.createButton({
title : 'Cancel',
style : Titanium.UI.iPhone.SystemButtonStyle.BORDERED
});
ThisControl.Done = Titanium.UI.createButton({
title : 'Done',
style : Titanium.UI.iPhone.SystemButtonStyle.DONE
});
ThisControl.Spacer = Titanium.UI.createButton({
systemButton : Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
});
ThisControl.Toolbar = Titanium.UI.createToolbar({
top : 0,
items : [ ThisControl.Cancel, ThisControl.Spacer, ThisControl.Done ]
});
ThisControl.Picker = Titanium.UI.createPicker({
top : 43
});
ThisControl.Picker.selectionIndicator = true;
ThisControl.Done.addEventListener('click', function() {
ThisControl.Log("Inside 'Controls.CreatePickerView' - Clicked 'Done'");
ThisControl.Hide();
done(ThisControl.Picker.getSelectedRow(0));
});
ThisControl.Cancel.addEventListener('click', function() {
ThisControl.Log("Inside 'Controls.CreatePickerView' - Clicked 'Cancel'");
ThisControl.Hide();
});
ThisControl.Picker.add(data);
ThisControl.View.add(ThisControl.Toolbar);
ThisControl.View.add(ThisControl.Picker);
wn.add(ThisControl.View);
ThisControl.Log("Inside 'Controls.CreatePickerView' - End");
return ThisControl;
};
@MorningZ
Copy link
Author

I'm not sure... sorry....

the code was written for many versions of Titanium ago.... something has obviously changed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment