使用uidatepicker编辑ios UITextField

ghi*_*boz 6 editing uidatepicker uitextfield uiviewcontroller ios

我有一个uitextfield写的日期..如何在页面底部显示一个uidatepicker而不是标准键盘?提前致谢

Eva*_*ski 5

确保您的类实现UITextFieldDelegate协议.然后,重写shouldBeginEditing委托方法以返回FALSE:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //check if it is the UITextField you don't want the keyboard for
    if (textField != dateTextField)
        return TRUE;

    //present your UIDatePicker
    //see instructions below

    //Return false to prevent the keyboard from appearing.
    return FALSE;
}
Run Code Online (Sandbox Code Playgroud)

我建议使用另一个UIViewController包含a UIDatePicker并用于presentModalViewController显示它.这符合用户界面指南.

  • 如果您只想在屏幕底部使用日期选择器,而不是覆盖整个屏幕,则此方法存在问题.您不能使模态呈现的视图控制器透明,因为底层视图不会存在.http://stackoverflow.com/questions/849458/transparent-modal-view-on-navigation-controller (2认同)

Osc*_*car 5

UITextField为此提供了一种机制.您应该能够将另一个视图(如datepicker)设置为inputView.看这篇文章:

iPhone datepicker而不是键盘?