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;
};
@theninthnode
Copy link

This looks awesome but I'm getting an error I haven't seen before:

[UIPickerView setFrame:]: invalid height value 228.0 pinned to 216.0

I get it when I try to use Controls.CreatePickerView(), do you know what's wrong?

@MorningZ
Copy link
Author

without seeing your code? no, i have no idea what is wrong..

keep in mind that i wrote up this code in mid-April... changes to Titanium may have varying affects on older code

@theninthnode
Copy link

Sorry for the crappy unhelpful comment. I have created the controls.js file with your code from above, I'm including the file and then creating the control like this:

Titanium.include('controls.js');

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 PickerApp = Controls.CreatePickerView(Titanium.UI.currentWindow, data, true, function (e) {});

I'm getting this error:

[INFO] Inside 'Controls.CreatePickerView' - Start

[INFO] Inside 'Controls.CreatePickerView' - End

2011-09-27 14:33:14.318 Hamils Quotation Application[1383:207] -[UIPickerView setFrame:]: invalid height value 228.0 pinned to 216.0

@pmackay
Copy link

pmackay commented Jan 16, 2012

I've tried this but my app has a tab bar at the bottom, and the view animation comes above the tab bar, reducing the remaining available screen space. Any idea how to get the popup to come from the bottom of the screen? Using the root window doesnt seem to work.

@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