に私はいくつかのiPhoneのプロジェクトで使用できる「ユーティリティ電子メールの送信者クラス」を作成しようとしています。MFMailComposeViewControllerは別のクラス
私はその目的のためにMailSenderのヘッダーと実装を作成しました。
MailSender.h:
@interface MailSender : NSObject<MFMailComposeViewControllerDelegate>
- (id) initWithParent:(UIViewController*) mainController;
- (void) invokeMailSender:(NSString*) to:(NSString*) subject:(NSString*) failureTitle:(NSString*) failureMessage:(NSString*) failureCancel;
@end
MailSender.m:
:私は、次の手順を使用して(ボタンにアクションをタッチダウン)ビューコントローラからクラスを呼ば#import "MailSender.h"
@implementation MailSender
MFMailComposeViewController* mailer;
UIViewController* mailParentController;
- (id) initWithParent:(UIViewController*) mainController
{
if(self = [super init])
{
mailParentController = mainController;
}
return self;
}
- (void) invokeMailSender:(NSString*) to:(NSString*) subject:(NSString*) failureTitle:(NSString*) failureMessage:(NSString*) failureCancel;
{
if([MFMailComposeViewController canSendMail])
{
mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:subject];
NSArray *toRecipients = [NSArray arrayWithObjects:to, nil];
[mailer setToRecipients:toRecipients];
[mailParentController presentModalViewController:mailer animated:YES];
}
else
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:failureTitle message:failureMessage
delegate:nil cancelButtonTitle:failureCancel otherButtonTitles: nil];
[alert show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// Do nothing
[mailParentController dismissModalViewControllerAnimated:YES];
mailer = nil;
}
@end
@implementation InfoViewController
MailSender *sender;
- (IBAction)openMail:(id)sender
{
sender = [[MailSender alloc] initWithParent:self];
[sender invokeMailSender:@"[email protected]" :@"123" :@"123" :@"123" :@"123"];
}
....
@end
私は、コードを実行すると、私は正しくメールビューを表示することができています。ただし、これに続いてクラッシュが発生します。 私のUIViewControllerからMFMailComposeViewControllerを直接使用するとクラッシュしないことに注意してください(デリゲートとしてView Controllerを割り当てます)
アイデアはありますか? は申し訳ありませんが、私はまだObjective Cのに新しいです:)
このコードでは問題はありません...うまく動作するはずです。クリーンビルドを試みるか、xcodeを再起動してください。私はそれがどこか他の場所からクラッシュしていると思う。 –
削除した場合:mailer.mailComposeDelegate = self;それはもうクラッシュしない!私の代議員に何か間違っていますか? – SiN
何時にクラッシュしますか? 'didFinishWithResult'デリゲートメソッドが何時でも呼び出されますか?また 'initWithParent'メソッドを呼び出すView Controllerのコードを投稿することもできますか? – Mutix