0

我创建了一个类来启动 MailComposer,这样我的 iPhone 应用程序在生成各种类型的电子邮件时只有一个地方可以去:一些带有附件,一些没有。有些带有预先填写的地址,有些则没有。

我不希望我的类实现 UIViewController,但它必须这样才能成为 MailComposer 的委托。否则,调用我的类的视图控制器本身必须是 MailComposer 的委托,这违背了目的。

让我的类成为视图控制器的缺点是它必须先加载到屏幕上,然后才能以模态方式调出 MailComposer。不幸的是,视图控制器不能是透明的。效果是,屏幕上的任何内容都会在 MailComposer 出现之前被纯白色视图控制器覆盖片刻。

我也许可以忍受,但不是这样:在 MailComposer 消失后,我的空白视图控制器占据了屏幕。我应该能够通过调用它来从内部摆脱它:

[self.parentViewController dismissModalViewControllerAnimated:NO];

但这死于可怕的死亡:“正在加载 43365 堆栈帧......”

我的班级——一个预先填充然后启动 MailComposer 的 UIViewController——是否丢失了它的 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];

如果您能帮助我,我将不胜感激!

4

2 回答 2

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];

然后在电子邮件 .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...];
}

希望有帮助。

于 2010-03-31T22:19:45.163 回答
0

我有同样的问题,我用不同的方式解决了它。

我创建了一个弹出当前 ViewController 的函数。在 h 中:

-(void)ics;

在 cpp 中:

-(void)ics{
  //[self.navigationController popViewControllerAnimated:NO];   
  [self.navigationController popToRootViewControllerAnimated:YES];
}

并在关闭 MailComposer 后调用它:

[self dismissModalViewControllerAnimated:YES];
[self ics];

瞧!

于 2010-04-20T13:23:01.657 回答