OGeek|极客世界-中国程序员成长平台

标题: ios - 从实用程序应用导航 Controller View 发送到实例错误的无法识别的选择器 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:06
标题: ios - 从实用程序应用导航 Controller View 发送到实例错误的无法识别的选择器

我有一个实用程序。我在反面实现了这段代码,它调用一个反馈 View 来发送电子邮件,就像这样 tutorial .这可行,但是当我单击发送反馈 UIButton 时,我的应用程序立即崩溃并显示 *** 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'-[UIViewController sendMail]: unrecognized selector sent to instance 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?如果是这样,我不知道该怎么做。

任何建议将不胜感激。



Best Answer-推荐答案


您错误地将类型 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4