开发者

iPhone UIDate popup?

开发者 https://www.devze.com 2023-03-07 11:59 出处:网络
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 the开发者_运

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 the开发者_运维技巧se 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?


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.


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消