ios - 从实用程序应用程序导航 Controller View 发送到实例错误的无法识别的选择器

标签 ios utility mfmailcomposer

我有一个实用程序应用程序。我在另一面实现了这段代码,它调用反馈 View 来发送电子邮件,如下所示 tutorial 。这是可行的,但是当我单击“发送反馈 UIButton”时,我的应用程序立即崩溃,并显示 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIViewController sendMail]:无法识别的选择器发送到实例 0x89c1960 '.

我已经检查过这些事情:

我已经正确声明了委托(delegate)并为 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 的 View Controller 而不是一个方法。我读过这个post这看起来非常相似,但我在 xib Identity 下拉列表中没有看到任何其他 View Controller 名称。我认为这是我的问题的一部分,但我不知道如何解决它。

也许我应该通过 View Controller 呈现 MFMailComposer?如果是这样,我不知道该怎么做。

如有任何建议,我们将不胜感激。

最佳答案

您错误地将类型UIViewController分配给您的自定义 View Controller 。您应该选择实际适用于您的自定义 View Controller 的类类型(包含方法实现 sendMail 的类类型)。

您的代码/设置的问题是您的自定义 View Controller 使用 UIViewController 类型进行实例化。但是,UIViewController 没有实现任何名为 sendMail 的方法,因此您会得到一个异常。

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

由于您似乎使用 InterfaceBuilder 来设置这些内容,因此请使用它来将 View Controller 的类型更改为 MyCustomViewController

enter image description here

编辑

从您的评论中,我可以看到您实际上使用代码实例化了 View Controller 。在这种情况下,请将您的方式替换为:

MyCustomViewController *controller = [[MyCustomViewController alloc] initWithNibName:@"ExMobSendFeedback" bundle:nil]; 
controller.title = @"Feedback"; 
[self.navigationController pushViewController:controller animated:YES];
[controller release];

关于ios - 从实用程序应用程序导航 Controller View 发送到实例错误的无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8399695/

相关文章:

ios - 无法通过当前模态转换将图像传递给另一个 UIViewController

ios - 从 appdelegate swift 访问 UIwebview

iOS 推送通知 - JavaPNS - keystore.p12 文件安全

ios - 创建应用内购买时,如何清除新测试的购买数据?

sockets - 向 IP 地址和端口发送二进制数据的免费工具?

c++ - 如何从返回类型为pair的C++函数返回等效的nullptr?

ios - NSUrl 在 NSString 中带有超链接?

iOS 等同于 XAML spy 和网络检查员?

iphone - 我们可以获取在 MFMailComposeViewController 中设置的发件人电子邮件地址吗?

ios - 使用 MFMailComposeViewController 时出现问题