4

Just want to know how I would make a UIPickerView in a UIActionSheet with a simple array.


Ok well I actually found out how to put it into an action sheet but I like your way better because it applies more to my app, thanks, but I want to also know how put the options into the UIPickerView, I am just hung up on that part of that. I already have an array with the colors: red, green, blue, yellow, black, etc in it but I want to know how to put that into the pickerview if I have already used initwithframe:? Please anyone help I know it is a stupid question but I'm racking my head on my $$$$$$ Macbook.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Jab
  • 26,853
  • 21
  • 75
  • 114

1 Answers1

14

You don't want to do that. Instead, create your UIPickerView in Interface Builder and connect it to an Outlet in your view controller. Add it as a subview of your main view and set its frame coordinates so that it is offscreen just below the bottom edge, x=0, y=480.

Then, when you want to display the picker, animate it onto the screen with something like:

[UIView beginAnimations:nil context:NULL];
[colorPicker setFrame:CGRectMake(0.0f, 416.0f, 320.0f, 216.0f)];
[UIView commitAnimations];

And then hide it when you're done picking with this:

[UIView beginAnimations:nil context:NULL];
[colorPicker setFrame:CGRectMake(0.0f, 480.0f, 320.0f, 216.0f)];
[UIView commitAnimations];

This will cause your picker to slide up from the bottom animated and slide back down when you're done.

I don't think adding a picker to a UIActionSheet, if it's even possible, is advisable.

Matt Long
  • 24,438
  • 4
  • 73
  • 99
  • 3
    Matt - Whilst this works well, it relies on having somewhere to add a button to dismiss the picker elsewhere on the screen (e.g. on the navigation bar). But if the picker were in an action sheet, it would be "self contained" for dismissal. – dermdaly Jan 10 '10 at 15:49
  • I was looking at using a picker with a done button just above it; here's a helpful link: http://stackoverflow.com/questions/1262574/add-uipickerview-a-button-in-action-sheet-how – petert Feb 09 '10 at 17:44
  • 6
    this isn't the best way to do it anymore. probably need to get the dimensions of the screen rather than hardcoding the Y values. – ninjaneer Oct 18 '12 at 05:04
  • I agree, @Ninja. With the new screen size this should be more dynamic now. – Matt Long Oct 19 '12 at 20:02
  • Agree with the above comments. This code is fragile and not well encapsulated. – memmons Nov 04 '12 at 20:09
  • i dont suggest this because a user can still touch anywhere on the screen, while UIActionSheet stuff, disables everything else. – Vaibhav Saran Mar 25 '13 at 11:57