2

我有一个实用程序。我已经在另一面实现了这个代码,它调用一个反馈视图来发送电子邮件,就像这个教程一样。这可行,但是当我单击发送反馈 UIButton 时,我的应用程序立即崩溃*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController sendMail]: unrecognized selector sent to instance 0x89c1960'.

我检查了这些东西:

我已经正确声明了委托并为 MailComposer 实现了它。

我的方法 sendMail 连接到按钮的 TouchUp 事件。

我的方法名称同意:

- (IBAction)sendMail;

- (IBAction)sendMail 
{
if ([MFMailComposeViewController canSendMail]) 
{
    MFMailComposeViewController *mfViewController = [[MFMailComposeViewController alloc] init];
    mfViewController.mailComposeDelegate = self;

    [self presentModalViewController:mfViewController animated:YES];

}
else 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Status:" message:@"Your phone is not currently configured to send mail." delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];

    [alert show];
}

}

代码不会到达此方法,因为我在方法实现的顶部设置了一个断点,但没有被调用。ViewDidLoad 处的断点也没有被激活。

仔细查看此错误:

reason: '-[UIViewController sendMail]: unrecognized selector sent to instance 

似乎它需要一个名为 sendMail 而不是方法的视图控制器。我读了这篇看起来非常相似的帖子,但我在 xib 身份下拉列表中没有看到任何其他视图控制器名称。我认为这是我的问题的一部分,但我不知道如何解决它。

也许我应该通过视图控制器展示 MFMailComposer?如果是这样,我不知道该怎么做。

任何建议,将不胜感激。

4

1 回答 1

3

您错误地将类型分配UIViewController给自定义视图控制器。您应该选择实际应用于您的自定义视图控制器的类类型(包含方法实现的那个sendMail)。

您的代码/设置的问题是您的自定义视图控制器被实例化为 type UIViewController。但是, UIViewController 没有实现任何方法调用sendMail,因此你得到一个异常。

由于您没有指定自定义视图控制器的类名,因此为了这个答案,我将简单地假设一个;MyCustomViewController

由于您似乎使用 InterfaceBuilder 来设置这些东西,因此使用它将视图控制器的类型更改为MyCustomViewController.

在此处输入图像描述

编辑

从您的评论中,我可以看到您实际上使用代码实例化了视图控制器。在这种情况下,请用以下方式替换您的方式:

MyCustomViewController *controller = [[MyCustomViewController alloc] initWithNibName:@"ExMobSendFeedback" bundle:nil]; 
controller.title = @"Feedback"; 
[self.navigationController pushViewController:controller animated:YES];
[controller release];
于 2011-12-06T12:19:42.617 回答