简体   繁体   English

使用uidatepicker编辑ios UITextField

[英]ios UITextField to edit with uidatepicker

I have a uitextfield where is written a date.. how can I show on the bottom of the page a uidatepicker instead of a standard keyboard? 我有一个uitextfield写了一个日期..我怎么能在页面底部显示一个uidatepicker而不是标准键盘? thanks in advance 提前致谢

Make sure your class implements the UITextFieldDelegate protocol. 确保您的类实现UITextFieldDelegate协议。 Then, override the shouldBeginEditing delegate method to return FALSE: 然后,覆盖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;
}

I recommend using another UIViewController that contains a UIDatePicker and use presentModalViewController to show it. 我建议使用另一个包含UIDatePicker UIViewController并使用presentModalViewController来显示它。 This conforms to the User-Interface Guidelines. 这符合用户界面指南。

UITextField provides a mechanism for this. UITextField为此提供了一种机制。 You're supposed to be able to set another view (like a datepicker) as the inputView. 您应该能够将另一个视图(如datepicker)设置为inputView。 See this post: 看这篇文章:

iPhone datepicker instead of keyboard? iPhone datepicker而不是键盘?

You can use the following code ; 您可以使用以下代码;

//first of all, you have to declare the datePicker and then use the following code; 

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[dateLabel resignFirstResponder]; //the textField that you will set the selected date
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component

pickerViewDate = [[UIActionSheet alloc] initWithTitle:@"Select the date!"
                                             delegate:self
                                    cancelButtonTitle:nil
                               destructiveButtonTitle:nil
                                    otherButtonTitles:nil];

datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your spesific mode

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; //or another LocaleIdentifier instead of en_US
[dateFormatter setDateFormat:@"dd.MM.yyyy"]; //desired format

[datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker

//now preparing the toolbar which will be displayed at the top of the datePicker
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle=UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
[barItems addObject:flexSpace]; // set the left of the bar

[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:datePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
}

and additional actions ; 和其他行动;

-(IBAction)dateChanged{
    NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
    [FormatDate setLocale: [[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US"]];
    [FormatDate setDateFormat:@"dd.MM.yyyy"];
    dateLabel.text = [FormatDate stringFromDate:[datePicker date]];
}

-(BOOL)closeDatePicker:(id)sender{
    [pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];
    [dateLabel resignFirstResponder];
    return YES;
}

-(IBAction)doneButtonClicked{
    [self closeDatePicker:self];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM