简体   繁体   English

UIDatePicker:获取出现在两个文本字段中的日期

[英]UIDatePicker: get the date to appear in two text fields

I have searched the internet and this site for many hours - I have found a number of similar questions, and have tried the suggestions people made, but I can't seem to get anything to work with the code I am using. 我已经在互联网和该站点上搜索了许多小时-我发现了许多类似的问题,并尝试了人们提出的建议,但是我似乎无法使用正在使用的代码。

Here is what is going on: 这是怎么回事:

I have two text fields (dateOpened and dateClosed) that I would like the date to appear in. When you click on each textfield the datepicker appears (I was able to get that part to work). 我有两个文本字段(dateOpened和dateClosed),我希望日期显示在其中。当您单击每个文本字段时,日期选择器就会出现(我能够使该部分正常工作)。 However, I am having an issue getting the date to appear in the dateClosed textfield. 但是,我在使日期显示在dateClosed文本字段中时遇到问题。

For dateOpened - when I click on the textfield, the datepicker comes up, I select a date, and it appears in that textfield. 对于dateOpened-单击文本字段时,出现日期选择器,我选择一个日期,该日期出现在该文本字段中。 It works great! 效果很好!

The Problem: 问题:

For dateClosed - when I click on the textfield, the datepicker comes up (works great), I select a date, but it appears in the dateOpened textfield, not dateClosed like it is supposed to. 对于dateClosed-单击文本字段时,出现日期选择器(效果很好),我选择了一个日期,但它显示在dateOpened文本字段中,而不是应该显示的dateClosed。

I would really appreciate any suggestions on how to get the date to appear in the dateClosed textfield. 我非常感谢有关如何使日期显示在dateClosed文本字段中的任何建议。 Thank you!!! 谢谢!!! :) :)

.h file: .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 file: .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 steps 3个步骤

  • Identify which text field to be filled [start date or end date],use a flag[using delegates ] 使用标志[使用委托]标识要填充的文本字段[开始日期或结束日期]
  • Show date picker and get date collected by user 显示日期选择器并获取用户收集的日期
  • according to the first methods identified result[flag] save the date value for further use accordingly as start date and end date[if loop] 根据第一种方法,确定result [flag]保存日期值以进一步用作开始日期和结束日期[if循环]

here your method didn't implement getting value to self.closedDate 在这里您的方法没有实现为self.closedDate获取价值

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

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