如何将DatePicker数据放入电子邮件正文中

时间:2014-09-28 23:32:32

标签: ios objective-c xcode uidatepicker

我有一些文本字段已填写,然后用于填充带有NSString的电子邮件正文。我已经为日期选择器的文本字段完成了相同的代码,但它不是输出的字符串,因此NSString不起作用。我已经尝试过使用NSDate,但我不确定如何像其他人一样使用它。这是代码:

声明变量:

 @interface HomeView ()<MFMailComposeViewControllerDelegate,UITextFieldDelegate>
{
    IBOutlet UITextField *txtClient;
    IBOutlet UITextField *txtTime;
    IBOutlet UITextView *txtNotes;
    IBOutlet UITextView *txtHardware;
    IBOutlet UIDatePicker *txtDate;
}
@end

填写电子邮件:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject:[userDefault objectForKey:@"Subject"]];

    [picker setToRecipients:[NSArray arrayWithObject:[userDefault objectForKey:@"Email"]]];


    NSString *string=[NSString stringWithFormat:@"Client: %@\n Time: %@ \n\n Notes: \n %@ \n Hardware: \n %@",txtClient.text,txtTime.text,txtNotes.text,txtHardware.text];
    txtClient.text = @"";
    txtTime.text = @"";
    txtNotes.text = @"";
    txtHardware.text = @"";
    [picker setMessageBody:string isHTML:NO];
    [self presentViewController:picker animated:YES completion:^{

    //Not working - input date data
    //NSDATE *string=[NSString stringwithformat:@Date: %@",txtDate.date];
    //txtDate.date = @"";

    }];

提前致谢。

1 个答案:

答案 0 :(得分:1)

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
 [formatter setDateFormat:@"MM/dd/yyyy"]; // this is the date format, you can also use    [NSDateFormater setDateStyle: ] and [NSDateFormatter setTimeStyle: ];
 NSString *dateString = [formatter stringFromDate:DATE ]; //replace DATE with your date object

如果使用setDateFormat:访问this页面查看所有字母代码是什么。

如果您正在使用setDateStyle:这些是所有选项以及它们的外观

   NSDateFormatterNoStyle     = no style
   NSDateFormatterShortStyle  = “11/23/37” or “3:30 PM”.
   NSDateFormatterMediumStyle = “Nov 23, 1937” or “3:30:32 PM”.
   NSDateFormatterLongStyle   = “November 23, 1937” or “3:30:32 PM PST”.
   NSDateFormatterFullStyle   = “Tuesday, April 12, 1952 AD” or “3:30:42 PM Pacific Standard Time”.