如何根据按下的按钮填充电子邮件收件人?

时间:2014-07-30 18:02:32

标签: ios email button

我有一个适用于3个不同办公室的应用程序,我希望能够选择通过电子邮件将结果整理数据发送到哪个办公室。我想为每个自动填充电子邮件收件人的办公室使用不同的按钮,然后最后的最后一个按钮将整理所有信息并将其全部附加到电子邮件中。有没有办法做到这一点?我找到了发送按钮,它填充了我无法解决的收件人。

这是我的电子邮件部分,但是由1个电子邮件地址填充,而不是依赖于按下按钮。

    - (IBAction)checkData:(id)sender
{




    unsigned int x,a = 0;
    NSMutableString *emailmessage;
    emailmessage = [NSMutableString stringWithFormat: @""];

    for (x=0; x<9; x++)
    {
        switch (x) {
            case 0:
                if (nameTextField.text == nil) {
                    [emailmessage appendString:@"Name, "];
                    a=1;

                }
                break;

            case 1:
                if (emailTextField.text == nil)
                {
                    [emailmessage appendString:@"Email Address, "];
                    a=1;
                }
                break;

            case 2:
                if (dateLabel.text == nil)
                {
                    [emailmessage appendString:@"Date and Time of Near Miss, "];
                    a=1;
                }
                break;


            case 3:
                if (locationTextField.text == nil)
                {
                    [emailmessage appendString:@"Location of Near Miss, "];
                    a=1;
                }
                break;

            case 4:
                if (locLabel.text == nil)
                {
                    [emailmessage appendString:@"GPS Location, "];
                    a=1;
                }
                break;

            case 5:
                if (observersTextField.text == nil)
                {
                    [emailmessage appendString:@"Observers Team, "];
                    a=1;
                }
                break;
            case 6:
                if (affectedTextField.text == nil)
                {
                    [emailmessage appendString:@"Affected Team, "];
                    a=1;
                }
                break;
            case 7:
                if (catLabel.text == nil)
                {
                    [emailmessage appendString:@"Rating Classification, "];
                    a=1;
                }
                break;
            case 8:
                if (onOffLabel.text == nil)
                {
                    [emailmessage appendString:@"Third Party?, "];
                    a=1;
                }
                break;
            case 9:
                if (mlabelcategory.text == nil)
                {
                    [emailmessage appendString:@"Category, "];
                    a=1;
                }
                break;
            case 10:
                if (messageTextView.text == nil)
                {
                    [emailmessage appendString:@"Observation Description, "];
                    a=1;
                }
                break;
            case 11:
                if (activityTextView.text == nil)
                {
                    [emailmessage appendString:@"Type of Work Activity, "];
                    a=1;
                }

                break;
            case 12:
                if (imageView.image == nil)
                {
                    [emailmessage appendString:@"Image, "];
                    a=1;
                }
                break;


            default:
                break;
        }


    }



    {
        name = nameTextField.text;
        emailaddress = emailTextField.text;
        date = dateLabel.text;

        location = locationTypeBtn.text;
        observers = originatorTypeBtn.text;
        affected = destinationTypeBtn.text;
        rating = catLabel.text;
        thirdparty = onOffLabel.text;
        category = categoryTypeBtn.text;
        message = messageTextView.text;
        activity = activityTextView.text;
        gps = locLabel.text;




        NSMutableString *nearmissreport;
        nearmissreport = [NSMutableString stringWithFormat: @"<br><br> <b>Name:</b> %@ <br> <b>Email Address:</b> %@ <br><br> <b>Date & Time of Near Miss:</b> %@ <br><br> <b>Location of Near Miss:</b> %@ <br> <b>GPS Location:</b> %@ <br><br> <b>Observers Team:</b> %@ <br> <b>Affected Team:</b> %@ <br><br> <b>Rating Classification:</b> %@ <br><br> %@ <br><br> <b>Category:</b> %@ <br><b>Observation Description:</b> %@ <br><br> <b>Type of Work Activity:</b> %@ <br><br><b>Image:</b><br>", name, emailaddress, date, location, gps, observers, affected, rating, thirdparty, category, message, activity];

        NSLog(@"Near Miss Report: %@", nearmissreport);

        NSMutableString *testoMail;
        testoMail = [NSMutableString stringWithFormat: nearmissreport];

        NSLog(@"%@", testoMail);


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

        [picker setSubject: rating];

        // Set up the recipients.
        NSArray *toRecipients = [NSArray arrayWithObjects:@"example@example.com",nil];

        //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com",@"third@example.com", nil];
        //NSArray *bccRecipients = [NSArray arrayWithObjects:@"four@example.com",nil];

        [picker setToRecipients:toRecipients];
        //[picker setCcRecipients:ccRecipients];
        //[picker setBccRecipients:bccRecipients];

        // Attach an image to the email.

        NSData *imageData = UIImagePNGRepresentation([imageView image]);
        [picker addAttachmentData:imageData mimeType:@"image/png" fileName:@"NearMiss"];

        // Fill out the email body text.
        //NSMutableString *emailBody;
        testoMail = [NSMutableString stringWithFormat: @"%@", testoMail];

        [picker setMessageBody:testoMail isHTML:YES]; //HTML!!!!!!

        // Present the mail composition interface.
        [self presentViewController:picker animated:YES completion:nil];
    }
}


// The mail compose view controller delegate method
- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError *)error
{
    [self dismissModalViewControllerAnimated:YES];
}

3 个答案:

答案 0 :(得分:0)

为什么不实现按下按钮时调用的方法,从传入的信息中解析出按钮的ID,然后相应地设置电子邮件的“to”字段?

答案 1 :(得分:0)

-(IBAction)buttonPressed:(id)sender{
UIButton *button = (UIButton *)sender;
NSLog(@"%d", [button tag]);
switch(button.tag) {
case 1 :
// set label.text = example1@example.com
break;
case 2 :
// set label.text = example2@example.com
break;
}
}

答案 2 :(得分:0)

试试这个

- (IBAction)sendEmail:(id)sender {
    NSMutableString *report = [NSMutableString stringWithFormat: @"<br><br> <b>Name:</b> %@ <br> <b>Email Address:</b> %@ <br><br> <b>Date & Time of Near Miss:</b> %@ <br><br> <b>Location of Near Miss:</b> %@ <br> <b>GPS Location:</b> %@ <br><br> <b>Observers Team:</b> %@ <br> <b>Affected Team:</b> %@ <br><br> <b>Rating Classification:</b> %@ <br><br> %@ <br><br> <b>Category:</b> %@ <br><b>Observation Description:</b> %@ <br><br> <b>Type of Work Activity:</b> %@ <br><br><b>Image:</b><br>", name, emailaddress, date, location, gps, observers, affected, rating, thirdparty, category, message, activity];
    NSLog(@"Near Miss Report: %@", report);

    NSMutableString *testoMail = [NSMutableString stringWithFormat:report];
    NSLog(@"%@", testoMail);

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

    [picker setSubject: rating];

    // Set up the recipients.
    NSArray *toRecipients;
    NSArray *ccRecipients;
    NSArray *bccRecipients;

    switch([sender tag]) {
        case 0 : {
            // set label.text = example1@example.com
            toRecipients = [NSArray arrayWithObjects:@"example@example.com",nil];
            ccRecipients = [NSArray arrayWithObjects:@"second@example.com",@"third@example.com", nil];
            bccRecipients = [NSArray arrayWithObjects:@"four@example.com",nil];
            break;
        }
        case 1 : {
            // set label.text = example2@example.com
            toRecipients = [NSArray arrayWithObjects:@"example@example.com",nil];
            ccRecipients = [NSArray arrayWithObjects:@"second@example.com",@"third@example.com", nil];
            bccRecipients = [NSArray arrayWithObjects:@"four@example.com",nil];
            break;
        }
        case 2 : {
            // set label.text = example2@example.com
            toRecipients = [NSArray arrayWithObjects:@"example@example.com",nil];
            ccRecipients = [NSArray arrayWithObjects:@"second@example.com",@"third@example.com", nil];
            bccRecipients = [NSArray arrayWithObjects:@"four@example.com",nil];
            break;
    }
}
    if(toRecipients.length >0) {
    [picker setToRecipients:toRecipients];
}

if(ccRecipients.length >0) {
    [picker setCcRecipients:ccRecipients];
}

if(bccRecipients.length >0) {
    [picker setBccRecipients:bccRecipients];
}

// Attach an image to the email.
NSData *imageData = UIImagePNGRepresentation([imageView image]);
[picker addAttachmentData:imageData mimeType:@"image/png" fileName:@"NearMiss"];

// Fill out the email body text.
//NSMutableString *emailBody;
testoMail = [NSMutableString stringWithFormat: @"%@", testoMail];

[picker setMessageBody:testoMail isHTML:YES]; //HTML!!!!!!

// Present the mail composition interface.
[self presentViewController:picker animated:YES completion:nil];

}