gpt4 book ai didi

ios - 如何为多个 UITextfields 使用单个 IOS UIDatePicker 对象

转载 作者:行者123 更新时间:2023-11-29 02:50:46 24 4
gpt4 key购买 nike

我有以下三个与 UIDatePickers 关联的 UITextfield。

self.taskDate.inputView=[self returnDatePickerView:01];
self.taskStartTime.inputView=[self returnDatePickerView:02];
self.taskEndtime.inputView=[self returnDatePickerView:03];

这是我的 DatePicker 方法

    - (UIView *)returnDatePickerView:(int)tag{
UIToolbar *toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar1.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *doneButton3 = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(taskDateDone)];
[toolBar1 setItems:[NSArray arrayWithObjects:doneButton3, nil]];
UIView *dateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
if(tag==01){
datePicker.datePickerMode=UIDatePickerModeDate;
}else{
datePicker.datePickerMode=UIDatePickerModeTime;
}
//Setting user selected date as the date picker default
[datePicker setDate:taskDate animated:YES];
[datePicker addTarget:self action:@selector(taskDatePicked:)forControlEvents:UIControlEventValueChanged];
[dateView addSubview:datePicker];
[dateView addSubview:toolBar1];
return dateView;
}

这是我的回调 taskDatePicked

- (void)taskDatePicked:(id)sender
{
NSDateFormatter *systemtimeFormatter = [[NSDateFormatter alloc] init];
NSLog(@"tag %ld",textFieldTag);
//[systemtimeFormatter setTimeZone:[NSTimeZone systemTimeZone]];
if(textFieldTag==01){
taskDate = datePicker.date;
[systemtimeFormatter setDateStyle:NSDateFormatterShortStyle];
self.taskDate.text=[[NSString alloc] initWithString:[systemtimeFormatter stringFromDate:taskDate]];
}else if(textFieldTag==02){
taskStartTime = datePicker.date;
[systemtimeFormatter setTimeStyle:NSDateFormatterShortStyle];
self.taskStartTime.text=[[NSString alloc] initWithString:[systemtimeFormatter stringFromDate:taskStartTime]];
NSLog(@"taskStartTime picked date %@",taskStartTime.description);
}else if(textFieldTag==03){
taskEndTime = datePicker.date;
[systemtimeFormatter setTimeStyle:NSDateFormatterShortStyle];
self.taskEndtime.text=[[NSString alloc] initWithString:[systemtimeFormatter stringFromDate:taskEndTime]];
}
[datePicker reloadInputViews];
}

但此方法并未调用所有 UITextfied。我在这里做错了什么。请推荐

最佳答案

尝试使用以下代码将单个日期选择器用作具有不同日期/时间格式的多个文本字段的输入 View 。

此处文本字段的输入 View 在文本字段委托(delegate)中分配。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

考虑 UIDatePicker *datePicker; 是常见的日期选择器,NSInteger textFieldTag; 保存当前文本字段的标签(两者都添加在界面扩展中)。

确保为文本字段分配了标签值,并为每个字段连接了委托(delegate)。然后在 viewDidLoad 方法中添加以下内容。

datePicker = [[UIDatePicker alloc] init];
[datePicker addTarget:self action:@selector(taskDatePicked:) forControlEvents:UIControlEventValueChanged];

添加文本字段委托(delegate),如:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
switch (textField.tag)
{
case 1:
datePicker.datePickerMode = UIDatePickerModeDate;
break;
case 2:
datePicker.datePickerMode = UIDatePickerModeTime;
break;
case 3:
datePicker.datePickerMode = UIDatePickerModeTime;
break;
default:
break;
}
textField.inputView = datePicker;
textFieldTag = textField.tag;
return YES;
}

并且随着日期选择器的变化触发的事件如下:

- (void)taskDatePicked:(id)sender
{

UITextField *textField = (UITextField *)[self.view viewWithTag:textFieldTag];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
dateformatter.dateStyle = NSDateFormatterMediumStyle;
switch (textFieldTag)
{
case 1:
[dateformatter setDateFormat:@"dd-MM-YYYY"];
break;
case 2:
[dateformatter setDateFormat:@"hh:mm a"];
break;
case 3:
[dateformatter setDateFormat:@"hh:mm a"];
break;
default:
break;
}
textField.text = [dateformatter stringFromDate:[datePicker date]];
}

关于ios - 如何为多个 UITextfields 使用单个 IOS UIDatePicker 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24569212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com