0

我似乎对某些事情不以为然,看起来应该很简单!

我现在如何使用用户在 NSUserDefaults 中输入的字符串填充电子邮件的正文?

基本上,还有一个按钮可以在 NSUserDetaults 中输入诸如“YES”之类的刺痛信息。如何使电子邮件正文存储那些字符串。

这是我目前拥有的代码:

- (IBAction)buttonPressed:(id)sender {
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"SYS"];
[controller setMessageBody:@"How do I make this the content of NSUSERDEFAULTS?" isHTML:NO];
controller.navigationBar.barStyle = UIBarStyleBlack;
[self presentViewController:controller animated:YES completion:nil];

任何帮助将不胜感激!

谢谢!

使用您的代码,字符串显示为:是,否,也许,没办法,

如何更改格式以使它们出现

是的,

不,

也许,

没门。

感谢你的宝贵时间!

4

2 回答 2

1

你想要的是一个换行符,可以用\n(换行符)来实现。

NSArray *storedStrings = [[NSUserDefaults standardUserDefaults] objectForKey:@"yourStoredKey"];
NSMutableString *messageBody = [NSMutableString string];
for (NSString *aString in storedStrings) {
    [messageBody appendFormat:@"%@,\n\n", aString];
}
[messageBody deleteCharactersInRange:NSMakeRange(messageBody.length-3, 3)];
[controller setMessageBody:messageBody isHTML:NO];

我在 for 循环之后添加的那一行将从字符串中删除最后三个字符,即两个换行符和逗号。

于 2013-04-02T21:35:54.277 回答
0

试试这个方法

- (IBAction)buttonPressed:(id)sender {
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"SYS"];
[controller setMessageBody:[[nsstring stringwithformat@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"yourStoredKey"]]  isHTML:NO];
controller.navigationBar.barStyle = UIBarStyleBlack;
[self presentViewController:controller animated:YES completion:nil];
于 2013-04-03T09:20:18.170 回答