2

On an Android device, if I wanted a date or number picker to popup, a window with the control on the popup. I haven't found a way to do this on iPhone? What is the recommended way to handle these objects?

EDIT: I have added a UIDatePicker using Interface Builder, but it's static and doesn't popup. What code would I use to make this popup?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark Worsnop
  • 4,407
  • 15
  • 54
  • 79

3 Answers3

4

iOS 3.2 and Later

UIResponder has an inputView property which can be used with UITextField and UITextView to replace the standard keyboard with any view you please, including a UIDatePicker. (See also: inputAccessoryView, which can be used to put a toolbar above the keyboard.)

See Custom Views for Data Input in the iOS documentation for more details.

iOS 3.0 and Earlier

If you want the picker to pop up as the keyboard does, you have to do this yourself. It's not too hairy.

To make the picker view slide into position from the bottom of the screen:

pickerView.frame = <offscreen, just below the main view>
[UIView beginAnimations:@"PickerUp" context:nil];
pickerView.frame = <onscreen, at the bottom of the view>;
[UIView commitAnimations];

To make the picker view slide away when done:

[UIView beginAnimations:@"PickerDown" context:nil];
pickerView.frame = <offscreen, just below the main view>
[UIView commitAnimations];

The keyboard is handled automagically because it is managed by UITextView/UITextField; since you want a custom input view, you have to write the code to manage it yourself.

If you go this route, you should look read the documentation about becomeFirstResponder and the whole UIResponder chain, so your custom control plays well with the system and other text fields.

If you want a toolbar above the picker, just create a generic UIView containing the toolbar, picker, and anything else you want to pop up from below. Then animate that container view in, instead of just the picker.

benzado
  • 82,288
  • 22
  • 110
  • 138
1

If you want to get a date from the user there is a UIDatePicker that will pop up a control to get a date input.

For a number picker you have a couple of options, you could collect the number in a UIAlertView containing a UITextField (this question describes how this would work) or you could use a UIPicker to select a number (similar to the UIDatePicker UI).

Community
  • 1
  • 1
RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
  • I put the datepicker on the view in IB. I guess there is a way to call this programmatically so it pops up? If I put a button in place of the UIDatePicker and have that button call the picker, how would I do that? – Mark Worsnop May 22 '11 at 22:55
1

You can consider setting them as inputViews on textFields and textViews. The input view will pop up from below and present itself for selection. You can look at a sample implementation I provided for question on similar lines here. You can also enable these on custom subclasses of UIResponder.

Community
  • 1
  • 1
Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • this looks like what I need! You dont by chance have a zip with the source? I tried to make this work, but I just don't know enough about iPhone yet and got more frustrated than it was worth! sure would appreciate it! – Mark Worsnop May 27 '11 at 21:39
  • I have put a sample class [`here`](http://pastie.org/1983801). Add it to your project. Then drag a text field on to the XIB and rename its class to `DateField` in the `Identity Inspector` and run the app. This should work but the thing it does is very minimal. Custom the class as per your needs. – Deepak Danduprolu May 28 '11 at 06:16