简体   繁体   English

出现键盘时MFMailComposeViewController didFinishWithResult问题

[英]MFMailComposeViewController didFinishWithResult issue when the keyboard appears

I've got a standard implementation of the MFMailComposeViewController. 我已经有了MFMailComposeViewController的标准实现。

I've set the right delegate protocols, and have a log happening on the didFinishWithResult method. 我已经设置了正确的委托协议,并且在didFinishWithResult方法上发生了日志。

See: 看到:

mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setSubject:emailTitle];
[mailComposer setMessageBody:messageBody isHTML:YES];
mailComposer.mailComposeDelegate = self;

[[self getController] presentViewController:mailComposer animated:YES completion:NULL];

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    NSLog(@"mail dismiss");
    [[self getController] dismissViewControllerAnimated:YES completion:NULL];
}

That shows the mail composer properly, and everything works well. 这说明邮件编辑器正常运行,并且一切正常。 Meaning, if I press the "cancel" button, the didFinishWithResult method gets called and the mailComposer gets dismissed. 意思是,如果我按下“取消”按钮,则didFinishWithResult方法将被调用,而mailComposer将会被关闭。

However, if I try to type anything, such as TO: email address, or anything else in the mail composer itself, it feels like the keyboard appearing is removing the delegate actions of my view controller, since the "cancel" and "send" buttons trigger no actions . 但是,如果我尝试键入任何内容,例如TO:电子邮件地址或邮件编辑器本身中的任何其他内容, 则感觉键盘出现正在删除我的视图控制器的委托操作,因为“取消”和“发送”按钮不会触发任何操作

Any thoughts? 有什么想法吗? It's driving me crazy :/ 这让我疯狂 :/

Cheers 干杯

EDIT 编辑

Here's the code for getController: 这是getController的代码:

- (UIViewController *) getController
{
    Class vcc = [UIViewController class];

    UIResponder *responder = self;
    while ((responder = [responder nextResponder]))
        if ([responder isKindOfClass: vcc])
            return (UIViewController *)responder;

    return nil;
}

When UITextField becomeFirstResponder, than your controller recieved resignFirstResponder. 当UITextField成为FirstResponder时,控制器将收到resignFirstResponder。

Why are you using method "getController"? 为什么使用方法“ getController”? Create property on ViewController which present MFMailComposeViewController. 在提供MFMailComposeViewController的ViewController上创建属性。

Set up a switch to catch the results form the mail composer like so: 设置一个开关来捕获来自邮件编写器的结果,如下所示:

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    switch (result) {
        case MFMailComposeResultCancelled:
            // User tapped cancel button
            break;
        case MFMailComposeResultSaved:
            // User saved email
            break;
        case MFMailComposeResultSent:
            // User sent email
            break;
        case MFMailComposeResultFailed:
            break;
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:NULL];
}

I'd recommend this implementation: 我建议此实现:

- (void) presentMailViewController
    if([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mail = [MFMailComposeViewController new];
        mail.mailComposeDelegate = self;
        mail.navigationBar.tintColor = self.navigationController.navigationBar.tintColor;;
        [self.navigationController presentViewController:mail animated:YES completion:nil];
    }   
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    if(error)
    {
       //...
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}   

And I'd recommend you to read this page: Code Naming Basics 我建议您阅读此页面: 代码命名基础

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM