我正在尝试显示电子邮件表单 appdelegate。
AppDelegate.h
@interface AppDelegate : UIResponder <MFMailComposeViewControllerDelegate, UIApplicationDelegate>
-(void)email;
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
...
-(void)email {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:@"Ear Dictation issue"];
NSArray *toRecipients = [NSArray arrayWithObject:@"info@musicrem.com"];
[mail setToRecipients:toRecipients];
NSString *emailBody = @"";
[mail setMessageBody:emailBody isHTML:NO];
mail.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentViewController:mail animated:YES completion:nil];//this line
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!"
message:@"E-mail is not supported on your device"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result) {
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
NSLog(@"mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"mail failed");
break;
}
[self dismissViewControllerAnimated:YES //this line
completion:nil];
}
...
我在标记的行上遇到了这个错误。
未找到实例方法“-presentViewController:animated:completion:”(返回类型默认为“id”)
和
未找到实例方法“-dismissViewControllerAnimated:completion:”(返回类型默认为“id”)
有人知道怎么解决吗?
最佳答案
这些是 UIViewController
中的方法。它们只能在 UIViewController
的实例上调用。你在你的应用程序委托(delegate)上调用它们不是 View Controller 。
解雇的解决方案很简单:
[controller dismissViewControllerAnimated:YES completion:nil];
显示邮件 Controller 的解决方案是用对适当 View Controller 的引用替换 self
。一种可能是:
[self.window.rootViewController presentViewController:mail animated:YES completion:nil];
这是否有效取决于您尝试显示邮件 Controller 时的 View Controller 结构。
关于ios - 找不到实例方法(电子邮件形式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20180463/