xcode 如何从 UITextfield 获取值并将其发送到电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11205750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get value from UITextfield and sent it to email?
提问by Dany
I've created an app which contains form and that have to filled up in appropiate text fields..now i need to get all data in textfield and it to be sent in email.here is my code for composing email
我创建了一个包含表单的应用程序,并且必须在适当的文本字段中填写..现在我需要在文本字段中获取所有数据并将其发送到电子邮件中。这是我编写电子邮件的代码
- (IBAction)compose:(id)sender
{
if (text1.text=@"" && text2.text=@"" && text3.text=@"" && text4.text=@"" && text5.text=@"") {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
message:@"Please enter all the field details"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
}
else{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:[NSString stringWithFormat:@"Appointment From Mr/Mrs. %@",text1.text]];
NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];
[mailer setToRecipients:toRecipients];
NSString *emailBody =[NSString stringWithFormat:@"Name =%@\nDate= %@\nPreferred Time Slot= %@\nE-Mail=%@\nSpecific Requests=",text1.text,text2.text,text3.text,text4.text,text5.text];
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
message:@"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
}
}
if all the text field is null it have to popup an alert.But i'm getting error?...guide me pls..
如果所有文本字段都为空,它必须弹出一个警报。但我收到错误?...请指导我..
回答by Omar Abdelhafith
If you are wondering how to get the text from textfield, and send it, you will need to use textfield.text
如果您想知道如何从 textfield 获取文本并发送它,您将需要使用 textfield.text
NSString *emailBody = textField.text;
[mailer setMessageBody:emailBody isHTML:NO];
回答by rishi
There can be two way -
可以有两种方式——
You can create proprty for your text field, and then access like -
myTextField.text
where you want.You can create a
NSString
property and set value in to this when text field has some text.
您可以为您的文本字段创建属性,然后像 -
myTextField.text
您想要的那样访问。NSString
当文本字段有一些文本时,您可以创建一个属性并为其设置值。
回答by Abhishek
//this is one single line which return your textFiledData
your_text_field.text
回答by Ravi
Create a variable for ur textfield like IBOutlet UITextField * textFieldVar; and connect it to ur Text field in the nib , then using the following code u can get the text from the text field
为您的文本字段创建一个变量,例如 IBOutlet UITextField * textFieldVar; 并将其连接到笔尖中的文本字段,然后使用以下代码您可以从文本字段中获取文本
NSString *emailBody = textFieldVar.text;
回答by Meet
You can create the body of email as you want by appending the string with particular format Like User ID: 223984775(value from the textfieldUserID) Name: XYZ(value from the textfieldName) ......
您可以根据需要通过附加具有特定格式的字符串来创建电子邮件正文,例如用户 ID:223984775(来自 textfieldUserID 的值)名称:XYZ(来自 textfieldName 的值)......
You can do this by appending the string as the user moves to the next text field to enter.
您可以通过在用户移动到下一个要输入的文本字段时附加字符串来完成此操作。
Use Nsstring "stringWithFormat" method to append the data in the body. Use \n and \t to give a Tabular structure to the body of email. Use formatting too
使用 Nsstring "stringWithFormat" 方法在正文中附加数据。使用 \n 和 \t 为电子邮件正文提供表格结构。也使用格式