0

その部分が機能しているアプリからメールを送信できるようにしたいです。問題は、電子メールの送信をキャンセルすると (これは現在テストしているものです)、電子メールの部分は閉じられますが、閉じられないように見える黒い画面が残ります。

これが私が持っているものです。メール部分を処理するクラスを作成しました。

MailViewController.h

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface MailViewController : UIViewController <MFMailComposeViewControllerDelegate>


@end

次のように実装されます。

#import "MailViewController.h"

@interface MailViewController ()

@end

@implementation MailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    if([MFMailComposeViewController canSendMail]){
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        [mailer setMailComposeDelegate:self];

        [mailer setSubject:@"Subject"];
        NSMutableArray *toArray = [[NSMutableArray alloc] initWithObjects:@"email@gmail.com", nil];
        [mailer setToRecipients:toArray];

        [self presentViewController:mailer animated:YES completion:nil];

    }
    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failuer" message:@"This device is unable to send emails" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{

    switch (result){
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email was sent");
            break;

        case MFMailComposeResultFailed:
            NSLog(@"Mail failed");
            break;

        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;

        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;

        default:
            NSLog(@"Mail not sent");
            break;
    }

    [controller dismissViewControllerAnimated:YES completion:nil];
}


@end

そして、これはすべて、次のように別のビューから表示されます。

-(IBAction)sendMessage:(id)sender{
    NSLog(@"Going to send an email....");

    MailViewController *mail = [[MailViewController alloc] init];

    [self.navigationController pushViewController:mail animated:YES];


}

このすべてを実行すると、電子メール プログラムが開き、電子メールの操作を行うことができます。[キャンセル] を選択してから [下書きを削除] を選択すると、電子メール プログラムが削除されます。ただし、黒い画面が残っているので、上部のナビゲーション バーから [戻る] を選択して前のビューに戻ることができます。

電子メールが送信またはキャンセルされたときに、電子メール プログラムを表示するビューにアプリが戻るようにしたいだけです。私が見逃しているのは単純なものだと確信しています。

4

2 に答える 2

1

問題はpushing、あなたが MailViewController であり、View Controller である必要があるdimissing場合です。pushpop

MFMailComposeViewControllers は、ナビゲーション スタックにプッシュするのではなく、モーダルに表示する必要があります。サブクラスも必要ありません。MFMailComposeViewController直接インスタンスを作成できます。

-(IBAction)sendMessage:(id)sender{
    NSLog(@"Going to send an email....");

    MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
    mail.mailComposeDelegate = self;
    [self presentViewController:mail animated:YES completion:nil];
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{

    switch (result){
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email was sent");
            break;

        case MFMailComposeResultFailed:
            NSLog(@"Mail failed");
            break;

        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;

        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;

        default:
            NSLog(@"Mail not sent");
            break;
    }

    [controller dismissViewControllerAnimated:YES completion:nil];
}
于 2012-10-18T19:41:30.797 に答える
0

私が持っているアーキテクチャの質問がいくつかありますが、最初に - TL;DR バージョン:

[self.navigationController popViewControllerAnimated:NO];

ブール値に YES を選択することもできますが、実装を判断すると、良い結果にはなりません。それをデリゲート呼び出しに入れて完了します。

しかし、MailViewController をスタックにプッシュしているものに、MFMailComposeViewControllerDelegate としての役割を処理させないのはなぜでしょうか? ここでは、まったく新しいビュー コントローラーをスタックにプッシュする必要があることは何もしていません。

于 2012-10-18T19:39:03.070 に答える