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];
}