我创建了一个类来启动MailComposer,这样我的iPhone应用程序在生成各种电子邮件时只有一个地方可去:一些带有附件,有些没有。一些有预先填写的地址,有些没有。
我不希望我的类实现UIViewController,但它必须这样才能成为MailComposer的委托。否则,调用我的类的视图控制器本身必须是MailComposer的委托,这会破坏目的。
让我的类成为视图控制器的缺点是它必须加载到屏幕才能以模态方式调出MailComposer。不幸的是,视图控制器不能透明。结果是,在MailComposer出现之前,屏幕上的任何内容都会被纯白色视图控制器覆盖。
我可以忍受,但不是这样:在MailComposer消失后,我的空白视图控制器占据了屏幕。我应该能够通过调用它来摆脱它本身:
[self.parentViewController dismissModalViewControllerAnimated:NO];
但那死得很可怕:“加载43365堆栈帧......”
我的类 - 一个UIViewController预先填充然后启动一个MailComposer - 丢失了它的parentViewController轨道?这不是零,因为我已经对此进行了测试。
从当前视图控制器中启动...
// My class is called Email.
Email *oEmail = [[[Email alloc] init] retain];
// Red, to remind myself that I'd like to someday learn to make it transparent.
oEmail.view.backgroundColor = [UIColor redColor];
// Pre-fill whatever fields you want, and specify attachments.
oEmail.EmailSubject = @"I am truly stumped";
// This has to go on screen first.
[self presentModalViewController:oEmail animated:NO];
// Then this can happen, which brings up the MailComposer.
[oEmail f_SendEmail];
// Commenting out the next line didn't help, so I turned it back on.
[oEmail release];
在类中,您需要mailComposeController:didFinishWithResult:error:方法使MailComposer消失,为此,该类必须是MFMailComposeViewControllerDelegate。这是在那里发生的事情:
// This gets rid of the mail composer.
[self dismissModalViewControllerAnimated:YES];
// This never fails to get rid of other modal view controllers when called
// from within those controllers, but boy does it not work here.
[self.parentViewController dismissModalViewControllerAnimated:NO];
如果你能帮助我,我将非常感激!
答案 0 :(得分:1)
而不是打电话
[self.parentViewController dismissModalViewControllerAnimated:NO];
我会为你的'电子邮件'控制器设置一个代表。 在创建新项目时,可以在“FlipSide”应用程序模板中看到此类连接的示例。
基本上,您需要为电子邮件控制器设置委托:
Email *oEmail = [[[Email alloc] init] retain];
oEmail.view.backgroundColor = [UIColor redColor];
oEmail.EmailSubject = @"I am truly stumped";
[self presentModalViewController:oEmail animated:NO];
[oEmail f_SendEmail];
[oEmail setDelegate:self];
[oEmail release];
然后在Email .h文件中:
@protocol EmailDelegate
-(void)emailDidFinish;
@end
@implementation Email : UIViewController {
// Other stuff
id <EmailDelegate> delegate;
}
@property (nonatomic, assign) id <EmailDelegate> delegate;
@end
确保你@synthesize委托,然后当你准备解雇它时:
// This gets rid of the mail composer.
[self dismissModalViewControllerAnimated:YES];
// This never fails to get rid of other modal view controllers when called
// from within those controllers, but boy does it not work here.
if (delegate && [delegate respondsToSelector:@selector(emailDidFinish)]){
[delegate emailDidFinish];
}
最后,在您的原始视图控制器中,确保您已进入.h文件,然后执行:
-(void)emailDidFinish {
[self dismissModal...];
}
希望有所帮助。
答案 1 :(得分:0)
我有同样的问题,我以不同的方式解决了它。
我创建了一个弹出当前ViewController的函数。 在h:
-(void)ics;
在cpp:
-(void)ics{
//[self.navigationController popViewControllerAnimated:NO];
[self.navigationController popToRootViewControllerAnimated:YES];
}
并在解雇MailComposer后调用它:
[self dismissModalViewControllerAnimated:YES];
[self ics];
瞧!