从tableView推送到MFMailComposerViewController

时间:2014-06-17 12:25:46

标签: ios objective-c uitableview mfmailcomposeviewcontroller

我正在开发一个应用程序,其中我有一个包含7个部分的静态tableView。这些部分是用户可以应用的不同课程,我想要完成的是当用户单击某个部分的行/按钮时。它会推送MailComposerView并在邮件内部,例如,

  

您好我想申请在此日期%@累积的课程%@。 %@是选定的课程和日期。

请告诉我如果需要添加更多信息/代码。

感谢。

2 个答案:

答案 0 :(得分:0)

<MessageUI/MessageUI.h><MessageUI/MFMailComposeViewController.h>导入您的viewController,并将MFMailComposeViewControllerDelegate添加到您的界面。

使用此代码显示MailComposeView

MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:@"<subject here>"]
[mailViewController setMessageBody:@"" isHTML:NO];
NSArray *toRecipients = [NSArray arrayWithObject:@"your.email@email.com"];
[mailViewController setToRecipients:toRecipients];

[self presentViewController:mailViewController animated:YES completion:nil];

这是为了驳回MailComposeView

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    NSLog(@"ERROR: %@", error);
    [self dismissViewControllerAnimated:YES completion:nil];
}

如果你想拥有不同的正文文本,那么把代码放在一个接受NSString参数的方法中,然后从按钮或行的选择方法中调用它,并为它提供你想要的文本。体。

答案 1 :(得分:0)

您需要使用UITableView的didSelectRowAtIndexPath:方法。尝试以下内容:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init]; 
    mailViewController.mailComposeDelegate = self;
    [mailViewController setSubject:@"Course Apply"]; //Set the subject here
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // this gets the currently selected cell.. Hope in your case, it's a custom cell. 
    [mailViewController setMessageBody:[NSString stringWithFormat:@"Hi I would like to apply to course %@ which accrues at this date %@",cell.courseSelected,cell.courseData] isHTML:NO]; //courseSelected and courseDate are properties that hold value in your custom cell
    NSArray *toRecipients = [NSArray arrayWithObject:@"your.email@email.com"];
    [mailViewController setToRecipients:toRecipients]; // set the recipient address here
    [self presentViewController:mailViewController animated:YES completion:nil]; // and finally present it... 
}

希望这会有所帮助..