ios - 找不到实例方法(电子邮件形式)

标签 ios objective-c xcode email appdelegate

我正在尝试显示电子邮件表单 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/

相关文章:

ios - 单例核心数据方法

android - AdMob:过滤 child 广告

ios - 为什么 UITextView 在调整大小后在错误的框架中绘制文本?

objective-c - Clang 静态分析器警告 "Null pointer argument in call to CFRelease"

iOS 6 - 如何在方向改变时运行自定义代码

objective-c - 将自动释放的 CGColor 返回到 ARC 的转换方法

ios - 有趣的重叠 UIScrollView 难题

iOS,核心数据 : set argument is not an NSSet error when counting an NSSet

java - 如何用 Java 开发 iPhone 应用程序?

ios - UICollectionView 在单元格之间画线