1

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

这是发生了什么:

我有两个文本字段(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];
}
4

1 回答 1

1

3 个步骤

  • 确定要填写的文本字段[开始日期或结束日期],使用标志[使用代表]
  • 显示日期选择器并获取用户收集的日期
  • 根据第一种方法确定的结果[标志]保存日期值以供进一步用作开始日期和结束日期[如果循环]

在这里,您的方法没有实现获得价值self.closedDate

于 2012-10-05T22:15:13.870 回答