Staram się stworzyć "klasę nadawców e-maili z narzędziami", którą mogę wykorzystać w kilku projektach iPhone.MFMailComposeViewController w oddzielnej klasie
Utworzono nagłówek i implementację MailSender do tego celu.
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
Zadzwoniłem do klasy z widoku kontrolera (w przycisku lądować działań) zgodnie z poniższymi instrukcjami:
@implementation InfoViewController
MailSender *sender;
- (IBAction)openMail:(id)sender
{
sender = [[MailSender alloc] initWithParent:self];
[sender invokeMailSender:@"[email protected]" :@"123" :@"123" :@"123" :@"123"];
}
....
@end
Po uruchomieniu kodu mogę poprawnie wyświetlać widoki wiadomości e-mail. Następuje jednak awaria. Należy zauważyć, że nie mam awarię podczas korzystania MFMailComposeViewController bezpośrednio z mojego UIViewController (I przypisując kontroler widoku jako delegata),
Wszelkie pomysły? Przepraszam, że wciąż jestem nowym celem C :)
Nie widzę problemu w tym kodzie ... powinien działać dobrze !!! po prostu spróbuj clean building/restartowanie xcode. Myślę, że to zawiesza się z innego miejsca. –
Jeśli usunę: mailer.mailComposeDelegate = self; to już się nie psuje! Coś nie tak z moim delegatem? – SiN
O której godzinie ulega awarii? Czy jest to przypadek, gdy metoda delegata 'didFinishWithResult' jest wywoływana? Czy mógłbyś też opublikować kod kontrolera widoku, który wywołuje metodę 'initWithParent'? – Mutix