1

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). However, I am having an issue getting the date to appear in the dateClosed textfield.

For dateOpened - when I click on the textfield, the datepicker comes up, I select a date, and it appears in that textfield. 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.

I would really appreciate any suggestions on how to get the date to appear in the dateClosed textfield. Thank you!!! :)

.h file:

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:

-(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];
}
  • 1
    The problem seems to be that you're only ever setting `self.openedDate` – Dan F Oct 05 '12 at 20:06
  • Thanks for answering my question. I noticed that, but when I add self.closedDate to the if statement, the date will appear in both textfields. But I think you are onto something. I think the problem has to be there. Thanks again! – user1723899 Oct 05 '12 at 20:37
  • I Think your question is [similar to this question][1] [1]: http://stackoverflow.com/questions/11612460/how-to-use-one-uipickerview-for-multiple-textfields-in-one-view Hope this will solve your problem – Kamleshwar Oct 05 '12 at 21:15
  • This one is also similar to your problem http://stackoverflow.com/questions/4324510/multiple-sources-for-uipickerview-on-textfield-editing – Kamleshwar Oct 05 '12 at 21:17
  • Thanks for getting back to me!! I think I am too inexperienced to be able to solve my issue based on the other solutions. I really think the problem is with my if statement, I just don't know how to fix it. Thanks again!! – user1723899 Oct 05 '12 at 21:37

1 Answers1

1

3 steps

  • 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]

here your method didn't implement getting value to self.closedDate

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101