我已经在互联网和网站上搜索了好几个小时 - 我发现了一些类似的问题,并尝试了人们提出的建议,但我似乎无法使用我正在使用的代码。
以下是发生的事情:
我有两个文本字段(dateOpened和dateClosed),我希望日期显示在。当你点击每个文本字段时,出现了datepicker(我能够让那部分工作)。但是,我在将日期显示在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];
}
答案 0 :(得分:1)
3个步骤
此处您的方法未实现获取self.closedDate