3

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:@"xxx@xxxx.com", 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..

Bot
  • 11,868
  • 11
  • 75
  • 131
Vishnu
  • 2,243
  • 2
  • 21
  • 44

5 Answers5

12

If you are wondering how to get the text from textfield, and send it, you will need to use textfield.text

NSString *emailBody = textField.text;
[mailer setMessageBody:emailBody isHTML:NO];
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • Suppose if we have more than one text field do we have to declare individual text field and to get all data in array right? – Vishnu Jun 26 '12 at 11:07
  • not array, use [NSString stringWithFormat:@"first = %@, second = %@", firstTextField.text, secondTextField.text]; and so on – Omar Abdelhafith Jun 26 '12 at 11:13
  • NSString *emailBody = @"",UITextField.text; [mailer setMessageBody:emailBody isHTML:NO];.....I have defined like this but it says error..here text is my variable.. – Vishnu Jun 26 '12 at 11:15
  • change this NSString *emailBody = @"",UITextField.text; to NSString *emailBody = textField.text; – Omar Abdelhafith Jun 26 '12 at 11:17
1

There can be two way -

  1. You can create proprty for your text field, and then access like - myTextField.text where you want.

  2. You can create a NSString property and set value in to this when text field has some text.

rishi
  • 11,779
  • 4
  • 40
  • 59
1
 //this is one single line which return your textFiledData

 your_text_field.text
Abhishek
  • 2,255
  • 1
  • 13
  • 21
1

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

 NSString *emailBody = textFieldVar.text;
Ravi
  • 1,759
  • 5
  • 20
  • 38
1

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) ......

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

Meet
  • 4,904
  • 3
  • 24
  • 39