我是iphone开发的新手。我创建了一个基于tabbar的应用程序。在第一个我想要显示电子邮件作曲家。我能够显示它,但取消和发送按钮不起作用,我不知道我哪里出错了。请帮帮我。这是我的代码。
- (void)viewDidLoad
{
[super viewDidLoad];
[self displayComposerSheet];
}
-(void)displayComposerSheet
{
picker = [[MFMailComposeViewController alloc] init];
[[picker navigationBar] setTintColor:[UIColor blackColor]];
picker.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail])
{
[picker setToRecipients:[NSArray arrayWithObjects:@"name@gmail.com",nil]];
[picker setSubject:@"Sample"];
}
[self.view addSubview:picker.view];
[self presentModalViewController:picker animated:YES];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
}
答案 0 :(得分:1)
您正在两次展示邮件作曲家。
删除该行:
[self.view addSubview:picker.view];
用以下代码替换下一行:
[self.navigationController presentModalViewController:picker animated:YES];
答案 1 :(得分:1)
如果您只添加mailcomposser的子视图,则必须将其从self.view中删除, 在您的代码中,您还要添加子视图并显示
如果您只使用[self.view addSubview:picker.view];
尝试删除它。
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[controller.view removeFromSuperview];
}
我仍然建议使用
当前MFMailComposeViewController [self.navigationController presentModalViewController:picker animated:YES];
,
并使用[self dismissModalViewControllerAnimated:YES];
将其解雇。
答案 2 :(得分:0)
设置MFMailComposeViewController的委托
MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc]init];
mailcomposer.mailComposeDelegate = self;
并使用此委派方法
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
}
答案 3 :(得分:0)
使用此代码:
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObjects:@"niftyapplications@gmail.com", @"support@niftysol.com", nil];
[controller setToRecipients:toRecipients];
[controller setTitle:@"Contact Us"];
controller.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:controller animated:YES];
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self becomeFirstResponder];
NSString *strMailResult;
switch (result)
{
case MFMailComposeResultCancelled:
strMailResult = NSLocalizedString(@"E-Mail Cancelled",@"");
break;
case MFMailComposeResultSaved:
strMailResult = NSLocalizedString(@"E-Mail Saved",@"");
break;
case MFMailComposeResultSent:
strMailResult = NSLocalizedString(@"E-Mail Sent",@"");
break;
case MFMailComposeResultFailed:
strMailResult = NSLocalizedString(@"E-Mail Failed",@"");
break;
default:
strMailResult = NSLocalizedString(@"E-Mail Not Sent",@"");
break;
}
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ISO Audit",@"") message:strMailResult delegate:self cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
[alertView show];
[self dismissModalViewControllerAnimated:YES];
}