IOS日期选择器像弹出式键盘功能? [英] IOS Date Picker Like A Popup Keyboard Functionality?

查看:144
本文介绍了IOS日期选择器像弹出式键盘功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到具有8个文本字段条目和4个日期字段条目的数据输入表单(可滚动视图),可以在正常情况下显示日期选择器(以及键盘弹出的方式),具体取决于用户点击哪个文本字段?



我想你可以使用代码以模态方式呈现日期选择器,但它似乎以与键盘相同的方式呈现日期选择器调用和呈现将是一个内置的SDK或我错过的东西? (在nib编辑器中设置一个属性来告诉这个文本字段接受日期的视图,并给出除了各种键盘之外的日期选择器选项)



它打破了我的想法,为什么开发人员没有一个简单的方式来弹出一个日历或日期选择器,当一个宣布的日期字段得到焦点。



任何你知道的示例应用程序或教程可以指向我吗?

解决方案

我刚刚研究了同样的问题,而网络充满了第三方图书馆和棘手的黑客,实际上是一个漂亮的简单,也许可能是完成你以后的首选方式。



首先,它利用 UITextField提供的功能 inputView 输入配件视图属性,共同提供无限的自定义可能性。对我来说,这个问题变得越来越了解如何组织起来,以达到我想要的目的。我建议在这里阅读:



https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html


因此,我开始声明两个属性:

  @property(nonatomic,strong)UIDatePicker * datePicker; 
@property(nonatomic,strong)UIView * datePickerToolbar;

,然后使用以下内容创建每个人:

   - (UIView *)datePickerToolbar {

if(!_datePickerToolbar){

UIToolbar * toolbar = [[ UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
toolbar.barStyle = UIBarStyleBlack;

UIBarButtonItem * cancelButton = [[UIBarButtonItem alloc] initWithTitle:@取消样式:UIBarButtonItemStylePlain target:self action:@selector(datePickerCanceled :)];
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@Donestyle:UIBarButtonItemStyleDone target:self action:@selector(datePickerDone :)];
UIBarButtonItem * space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

[toolbar setItems:[NSArray arrayWithObjects:cancelButton,space,doneButton,nil] animated:NO];

_datePickerToolbar =工具栏;
}
return _datePickerToolbar;
}

- (UIDatePicker *)datePicker {

if(!_datePicker){

UIDatePicker * datePicker = [[UIDatePicker alloc ] 在里面];
datePicker.datePickerMode = UIDatePickerModeDate;

datePicker.date = [NSDate date];

_datePicker = datePicker;
}
return _datePicker;
}

当然,您可以自定义这些视图以满足您的需要。然而,由于这些视图是您自己的,您还需要确定工具栏中的每个按钮应该如何处理交互。虽然这两个都应该解除键盘(这是通过撤销第一个响应者完成的),在我的情况下,我也想要完成按钮将日期输入字段设置为所选日期的格式化字符串:

   - (void)datePickerCanceled:(id)sender {
// just resign focus and dismiss date picker
[self.dateOfBirthField resignFirstResponder] ;
}


- (void)datePickerDone:(id)sender {

NSDate * selectedDate = self.datePicker.date;

//格式化日期到字符串
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

//格式整体字符串
NSString * dateString = [dateFormatter stringFromDate:selectedDate];
self.dateInputField.text = dateString;

[self.dateOfBirthField resignFirstResponder];
}

之后,唯一需要做的是触发键盘替换将我们的自定义视图分配给我之前提到的属性 UITextField 属性,我在ViewDidLoad中选择了它:

   - (void)viewDidLoad {
[super viewDidLoad];

self.dateInputField.inputView = self.datePicker;
self.dateInputField.inputAccessoryView = self.datePickerToolbar;
}

我希望这是你正在寻找的。看完所有内容后,我相信这是最简单,最易于管理的方法。我也喜欢这种方法,因为它可以被修改和重新使用,足以满足不同的输入要求。



在我看来,隐藏的宝石。


Considering a data entry form (scrollable view) that has 8 text field entries and 4 date field entries, Is it possible to present a date-picker at the bottom where normally (and how normally a keyboard would pop up), depending on which text field the user tapped into?

I suppose you can present a date picker modally with code, but it seems presenting a date-picker the same way a keyboard is called and presented would be a built in to the SDK or am I missing something? (set an attribute in the nib editor to tell the view that this text field accepts a date, and to give you a date picker option in addition to the variety of keyboards)

It blows my mind why developers don't have a simple way to "pop-up" a calendar or date picker when a declared date field gets the focus.

Any example apps or tutorials that you know of you can point me towards?

解决方案

I just researched this same question, and while the web is replete with 3rd party libraries and tricky hacks, there's actually a pretty simple, and probably preferred way of accomplishing what you're after.

First of, it leverages the capabilities offered by the UITextField inputView and input accessoryView properties, which together offer endless customization possibilities. The problem for me became knowing how to structure things to get exactly what I wanted. I suggest reading about them here:

https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html

As such, I started by declaring two properties:

@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) UIView *datePickerToolbar;

and then created each of them with something like the following:

- (UIView*)datePickerToolbar {

    if (!_datePickerToolbar) {

        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        toolbar.barStyle = UIBarStyleBlack;

        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(datePickerCanceled:)];
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(datePickerDone:)];
        UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

        [toolbar setItems:[NSArray arrayWithObjects:cancelButton, space, doneButton, nil] animated:NO];

        _datePickerToolbar = toolbar;
    }
    return _datePickerToolbar;
}

- (UIDatePicker*)datePicker {

    if (!_datePicker) {

        UIDatePicker *datePicker = [[UIDatePicker alloc] init];
        datePicker.datePickerMode = UIDatePickerModeDate;

        datePicker.date = [NSDate date];

       _datePicker = datePicker;
    }
    return _datePicker;
}

And of course, you can customize these views to suit your needs. Since these views are your own, however, you'll also need to decide how each button in the toolbar should handle interaction. While both should dismiss the "keyboard" (which is done by resigning first responder), in my case I also wanted the Done button to set the date input field to a formatted string of the selected date:

- (void)datePickerCanceled:(id)sender {
    // just resign focus and dismiss date picker
    [self.dateOfBirthField resignFirstResponder];
}


- (void)datePickerDone:(id)sender {

    NSDate *selectedDate = self.datePicker.date;

    // format date into string
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

     // format overall string
    NSString *dateString = [dateFormatter stringFromDate:selectedDate];
    self.dateInputField.text = dateString;

    [self.dateOfBirthField resignFirstResponder];
}

After that, the only thing left to do to trigger our keyboard replacement is to assign our custom views to those properties UITextField properties I mentioned earlier, which I chose to do in 'ViewDidLoad`:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.dateInputField.inputView = self.datePicker;
    self.dateInputField.inputAccessoryView = self.datePickerToolbar;
}

I hope this is what you were looking for. After seeing everything out there, I'm convinced that this is the simplest, most manageable way to do it. I also like this approach because it can be modified and repurposed easy enough for different input requirements.

A hidden gem, in my opinion.

这篇关于IOS日期选择器像弹出式键盘功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆