gpt4 book ai didi

iphone - UIDatePicker:获取显示在两个文本字段中的日期

转载 作者:行者123 更新时间:2023-11-29 13:27:31 35 4
gpt4 key购买 nike

我已经在互联网和这个网站上搜索了好几个小时——我发现了许多类似的问题,并尝试了人们提出的建议,但我似乎无法使用我正在使用的代码来解决任何问题。

事情是这样的:

我有两个文本字段(dateOpened 和 dateClosed),我希望在其中显示日期。当您单击每个文本字段时,会出现日期选择器(我能够使该部分工作)。但是,我在将日期显示在 dateClosed 文本字段中时遇到问题。

对于 dateOpened - 当我点击文本字段时,日期选择器出现,我选择一个日期,它出现在该文本字段中。效果很好!

问题:

对于 dateClosed - 当我点击文本字段时,日期选择器出现(效果很好),我选择了一个日期,但它出现在 dateOpened 文本字段中,而不是应该出现的 dateClosed。

对于如何让日期显示在 dateClosed 文本字段中的任何建议,我将不胜感激。谢谢你!!! :)

.h文件:

UIActionSheet *dateSheet;

@property (nonatomic, strong)NSDate *openedDate;
@property (nonatomic, strong)NSDate *closedDate;

@property (strong, nonatomic) IBOutlet UITextField *dateOpened;
@property (strong, nonatomic) IBOutlet UITextField *dateClosed;

-(IBAction)dismissKeyboard:(id)sender;

-(void)setOpened;
-(void)setDateField;
-(void)cancelDateSet;

.m 文件:

-(void)setOpened { 
dateSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[dateSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 44, 0, 0);
UIDatePicker *dateOpenedPicker = [[UIDatePicker alloc]initWithFrame:pickerFrame];
[dateOpenedPicker setDatePickerMode:UIDatePickerModeDate];
[dateSheet addSubview:dateOpenedPicker];
UIToolbar *controlToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleBlack];
[controlToolBar sizeToFit];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc]initWithTitle:@"Set Date" style:UIBarButtonItemStyleDone target:self action:@selector(dismissDateSet)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelDateSet)];
[controlToolBar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil]animated:NO];
[dateSheet addSubview:controlToolBar];
[dateSheet showFromTabBar:self.tabBarController.tabBar];
[dateSheet setBounds:CGRectMake(0, 0, 320, 485)];
}

-(void)cancelDateSet {
[dateSheet dismissWithClickedButtonIndex:0 animated:YES];
}

-(void)setDateField {

NSArray *datesList = [dateSheet subviews];
for (UIView *subView in datesList)
{
if ([subView isKindOfClass:[UIDatePicker class]]) {
self.openedDate = [(UIDatePicker *)subView date];
}
}

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];

//will display date in dateOpened text field
[dateOpened setText:[dateFormatter stringFromDate:self.openedDate]];

//date to display in dateClosed text field - Does not work
[dateClosed setText:[dateFormatter stringFromDate:self.closedDate]];

[dateSheet dismissWithClickedButtonIndex:0 animated:YES];
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self setOpened];
return NO;
}

-(IBAction)dismissKeyboard:(id)sender {
[sender resignFirstResponder];
}

最佳答案

3个步骤

  • 确定要填写的文本字段[开始日期或结束日期],使用标志[使用委托(delegate)]
  • 显示日期选择器并获取用户收集的日期
  • 根据第一种方法确定的结果[flag]保存日期值以供进一步使用相应地作为开始日期和结束日期[if loop]

此处您的方法未实现获取 self.closedDate 的值

关于iphone - UIDatePicker:获取显示在两个文本字段中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12753280/

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