iphone - 添加MFMailComposeViewController(应用内电子邮件)的视图

问题描述:

我花了两天的时间试图在我的应用中启用发送电子邮件。希望这里的聪明人能够帮助我。 presentModalViewController不适用于我(只是崩溃的应用程序没有解释为什么),所以我不得不添加MFMailComposeViewController的视图。这是我的尝试:iphone - 添加MFMailComposeViewController(应用内电子邮件)的视图

MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init]; 
    controller.mailComposeDelegate = self; 
    [controller setSubject:@"test subject"]; 
    [controller setMessageBody:@"this is the message body" isHTML:NO]; 

    // [self presentModalViewController:controller animated:YES]; //this crashes the app 
//so I try this instead:   
    controller.view.frame = CGRectMake(0,0,480,320); 
    [self.view addSubview:controller.view]; 
    [controller release]; 

什么被添加到屏幕上是主题栏只有取消和发送按钮。没有任何文本字段(To :, Cc:,Subject,body)被显示。为什么他们不是MFMailComposeViewController视图的一部分,我该如何显示它们?

老实说,你应该使用presentModalViewController。考虑调试崩溃,而不是强迫你使用SDK。打开调试器并查看是否有任何异常记录在控制台中。检查崩溃日志等...

此外,请确保self是一个正确的委托和UIViewController子类。

而应该尝试:

[[self navigationController] presentModalViewController...];

因为这是展示它的正确方法。尝试手动添加其视图不幸的是完全不正确,将永远不会工作。

+0

我不使用导航控制器。无论如何我都试过这个代码 - 它不会崩溃,但没有任何反应。 – sol 2009-07-05 00:35:28

+0

您*必须*使用导航控制器来使用MFMailComposeViewController。它什么都不做的原因是因为如果你不使用导航控制器,[self navigationController]返回nil,并且在Cocoa消息中nil是无操作的。 – 2009-07-07 02:30:56

+0

您不应该需要使用导航控制器。亚当,你把'presentModal'和'pushViewController'混淆了吗? (后者需要导航控制器。) – 2010-03-07 08:37:18

嗯,我已经确定,必须创建一个虚拟视图控制器,否则混账东西不会滑动。

我创建一个名为Sys_Mail类,它是一个@interface Sys_Mail : UIViewController <MFMailComposeViewControllerDelegate>

,然后我基本上是创建根视图视图控制器。我与肖像/风景搏斗了数小时,但确定如果您将视图控制器附加到顶层视图(其中包含我的景观变换),那么它将作为横向窗口滑入。只有一个视觉故障,父窗口移动了几秒钟,而新窗口滑入,这是一个副作用的景观变换做奇怪的事情父母....

为了得到滑动窗口上横向您必须在Sys_Mail类来处理自动旋转消息宣布的方法:

//======================= 
// shouldAutorotateToInterfaceOrientation 
//======================= 
// see if this ever gets called for the view controller 
-(BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation 
{ 
    if (TRACE) printf ("shouldAutorotateToInterfaceOrientation\n"); 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); // or whatever orientation is needed 
} 

变量gMasterView指的是我的*别视图(具有景观改造和连接到窗口)。子视图似乎不起作用,视图控制器是可怕的他们更多的设计模式CRAP。我想完全控制我的观点,而不是一些微软的MFC类型的东西!

Sys_Mail* g_root_vc; 

if (g_root_vc == nil) { 
    // create an empty view controller so we have something to work with 
    g_root_vc = [[Sys_Mail alloc] init]; 
    g_root_vc.view = (UIView*) gMasterView; 
} 

所以这

我有同样的碰撞和最后我可以通过发送presentModalViewController消息[自我navigationController]修复它。

这里是我的代码:

// Create the Mail composer view controller 
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init]; 

// Set the view controller delegate 
controller.mailComposeDelegate = self; 

// Set recipients, if you want 
[controller setToRecipients:recipients]; 

// Set subject, if you want 
[controller setSubject:@"The subject"]; 

// Set message body, if you want 
[controller setMessageBody:@"The message body" isHTML:YES]; // isHTML -> YES/NO depending the message body 

// Present the view controller 
[[self navigationController] presentModalViewController:controller animated:YES]; 

// Memory management 
[controller release]; 

我希望这可以帮助!

我已经解决了这个问题:

尽量不要这样:

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init]; 

但这:

MFMailComposeViewController *mailComposeViewController = [MFMailComposeViewController new];