2010-03-31 62 views
0

我創建了一個類來啓動MailComposer,這樣我的iPhone應用程序在生成各種電子郵件時只有一個地方可用:一些帶附件,一些不帶。有些預填充地址,有些則不是。iPhone MailComposer類UIViewController dismissModalViewControllerAnimated問題

我不希望我的類實現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:錯誤:方法使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

請注意,「加載43365堆棧幀...」表示無限遞歸。如果你看看調試器,你應該看到造成這種情況的方法週期。 – 2010-04-01 12:23:13

+1

另外,「視圖控制器不能透明」在技術上是錯誤的。視圖控制器只是管理視圖邏輯的對象。視圖本身可以是透明的,半透明的或非全屏的。你所指的是-presentModalViewController:animated:方法,它在你的控制器視圖背後放置一個不透明的白色視圖,當它滑動到屏幕上時。 – 2010-04-01 12:26:46

回答

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

希望有所幫助。

+0

我會測試這個。耐人尋味。我真的不在乎委託,因爲如果一個對象必須將責任交給其他對象,那麼對象怎麼能夠自成一體呢?但是,MailComposer是Apple創建的對象,沒有關聯的代碼模塊,我們可以添加自定義代碼。所以我們讓其他對象代表,然後可以在那裏寫我們的代碼。麻煩的是,您必須將該代碼放在每個可能顯示MailComposer的ViewController中。我無法確定你的建議解決方案是否可以解決這個問題,直到我測試。 – 2010-04-01 04:07:33

0

我有同樣的問題,我解決了它不同的方式。

我創建了一個彈出當前ViewController的函數。 在H:

-(void)ics; 

在CPP:

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

,並把它稱爲解聘MailComposer後:

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

瞧!