我的应用程序有一个textField,当按下该按钮时,会打开一个datePicker供用户选择日期,然后将此信息发送到标签。我只需要获取DATE值,但发送的字符串类似于:
<UITextField: 0x7ff66e740bf0; frame = (16 12; 289 30); text = '08/03/2017'; opaque = NO; (...and so on...)
如何才能获得DATE字符串?
我的代码是:
- (void)viewDidLoad
{
datePicker=[[UIDatePicker alloc]init];
datePicker.datePickerMode=UIDatePickerModeDate;
[self.dia2 setInputView:datePicker];
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolBar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Pronto" style:UIBarButtonItemStyleBordered target:self action:@selector(ShowSelectedDate)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
[self.dia2 setInputAccessoryView:toolBar];
}
-(void)ShowSelectedDate
{
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"dd/MM/YYYY"];
self.dia2.text=[NSString stringWithFormat:@"%@",[formatter stringFromDate:datePicker.date]];
mostraData.text=[NSString stringWithFormat:@"%@", _dia2];
[self.dia2 resignFirstResponder];
}
答案 0 :(得分:0)
mostraData.text=[NSString stringWithFormat:@"%@", _dia2];
将文本视图的描述分配给标签,而不是刚刚添加到文本视图中的格式化字符串。
mostraData.text = _dia2.text;