【问题标题】:How to Populate an email recipient depending on which button is pressed?如何根据按下的按钮填充电子邮件收件人?
【发布时间】:2014-07-30 18:02:32
【问题描述】:

我有一个适用于 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];
}

【问题讨论】:

  • 使用button.tag 来识别按下了哪个按钮。 :)
  • 嗨@AbhishekBedi,我想你不知道有什么好的例子可以告诉我如何使用button.tag?
  • 我看不到 button.tag 如何填充收件人电子邮件?

标签: ios email button


【解决方案1】:

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

【讨论】:

  • 因此,例如,当按下按钮时,它会使用电子邮件地址填充 LabelA,然后让它查看 LabelA 而不是 example@example.com?
  • 我以为您正在尝试制作 MFMail 撰写对话框。你能多解释一下你所说的“labelA”是什么意思吗?
  • 对不起,我在想的是,如果按下按钮时它会填充一个标签,例如 LabelA,然后我可以使用 LabelA 中的结果来填充电子邮件收件人吗?例如,如果按下显示“Office 1”的按钮,则标签将填充电子邮件地址 example1@example.com 如果按下“Office 2”按钮,则标签将填充 example2@example.com。或者这是一种非常冗长的做法?
  • 你的 99%,你的问题是你不知道如何识别按下了哪个按钮。我能想到的方法至少有 4 种。我的问题是 3 个按钮的属性?他们有像 emailBossButton 这样的名字吗?要解决的另一件事是所有按钮都指向同一个 IBAction 吗?另一个问题,您显示的代码在哪里调用?在 IBAction 方法中?
  • 无需执行填充标签的中间步骤。我会将所有三个按钮连接到同一个 IB 操作,然后根据按下的按钮的 ID 填充 MFMailComposer 的 to 字段的 switch 语句。通过这个 IB 操作,您将能够展示 MFMail vc。
【解决方案2】:
-(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;
}
}

【讨论】:

  • 对不起,我现在完全迷路了。是否可以将我的代码添加到此以显示我放入 *toRecipients 以使其正常工作?非常感谢
【解决方案3】:

试试这个

- (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];

}

【讨论】:

  • 我猜这将根据您按下的按钮发送给任何收件人?我想要实现的是电子邮件收件人的选择是作为发送电子邮件的单独操作完成的。因此,也许在应用程序的顶部,您按下您所在办公室的按钮 A,填写应用程序的其余字段,然后在底部您有单独的发送按钮,它将接收您刚刚输入的所有信息并附加它发送到电子邮件。我可以用这段代码实现吗?非常感谢
  • 我已经用完整的电子邮件代码重新措辞了我最初的问题,希望它更清楚。我不认为我清楚我上次之后的情况,对此感到抱歉。
  • 好吧,我不确定您的编码有多强大,但您可以将我的部分答案复制并粘贴到您的方法中。 1] 创建一个字符串类属性来保存电子邮件地址(ess) 2] 创建一个填充该字符串属性的方法(使用 switch 语句) 3] 创建一个使用字符串属性作为收件人数组的方法并发送电子邮件 Everything你需要的是我和其他人在这里写的例子。如果您想分开设置收件人,只需创建一个单独执行此操作的方法。有什么帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 2016-05-19
  • 2012-10-01
  • 2013-08-06
相关资源
最近更新 更多