【问题标题】:iphone - adding the view of MFMailComposeViewController (in-app email)iphone - 添加 MFMailComposeViewController 的视图(应用内电子邮件)
【发布时间】:2010-11-08 04:00:20
【问题描述】:

过去两天我一直在尝试启用从我的应用程序中发送电子邮件。希望这里的聪明人之一可以帮助我。 presentModalViewController 对我不起作用(只是让应用程序崩溃而没有解释原因),所以我不得不添加 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];

添加到屏幕上的只是主题栏,带有取消和发送按钮。不显示任何文本字段(收件人:、抄送:、主题、正文)。为什么它们不是 MFMailComposeViewController 视图的一部分,我该如何显示它们?

【问题讨论】:

    标签: iphone email uiviewcontroller


    【解决方案1】:

    我已经解决了这个问题:

    不要这样尝试:

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

    但是这个:

    MFMailComposeViewController *mailComposeViewController = [MFMailComposeViewController new];
    

    【讨论】:

      【解决方案2】:

      好吧,我已经确定必须创建一个虚拟视图控制器,否则该死的东西不会滑入。

      我创建了一个名为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 指的是我的顶级视图(具有景观变换并附加到窗口)。子视图似乎不起作用,视图控制器很糟糕,它们更像是设计模式垃圾。我想要完全控制我的视图,而不是一些微软 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; 
      }
      

      所以这个

      【讨论】:

        【解决方案3】:

        我有同样的崩溃,最后我可以通过向 [self navigationController] 发送 presentModalViewController 消息来修复它。

        这是我的代码:

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

        希望对你有帮助!

        【讨论】:

        • 我也面临同样的问题,控制器变成零,“0x0”所以它崩溃了。我不知道为什么它变成 nil 并存储 0x0 地址。
        【解决方案4】:

        老实说,您应该使用presentModalViewController。与其强行绕过 SDK,不如考虑调试崩溃。打开调试器,查看控制台中是否记录了任何异常。检查崩溃日志等...

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

        【讨论】:

        • self 确实是 UIViewController 的子类和适当的委托:@interface MenuViewController : UIViewController { ...崩溃时没有记录异常。这就是为什么我在这里完全不知所措。我只是得到一个黑屏。我可以从控制台以外的地方获取崩溃日志吗?
        • 检查 XCode 中的 Organizer。
        • 还有 ~/Library/Logs/DiagnosticReports/
        • 我已经发布了一个调试图像,显示了 presentModalViewController 行中的代码。有一个红色的十六进制值,但我不确定这是否有问题:img127.imageshack.us/img127/6329/picture2dla.png
        • 另外,我的应用程序只是横向的。不确定这是否会导致 presentModalViewController 出现问题。
        【解决方案5】:

        您应该尝试:

        [[self navigationController] presentModalViewController...];

        因为这是呈现它的正确方式。不幸的是,尝试手动添加其视图是完全不正确的,并且永远不会起作用。

        【讨论】:

        • 我没有使用导航控制器。无论如何,我尝试了该代码 - 它没有崩溃,但没有任何反应。
        • 必须使用导航控制器才能使用 MFMailComposeViewController。它什么都不做的原因是因为如果你不使用导航控制器,[self navigationController] 返回 nil,而在 Cocoa 中给 nil 的消息是无操作的。
        • 您不需要使用导航控制器。亚当,您是否将“presentModal”与“pushViewController”混淆了? (后者需要一个 navigationController。)
        • 格雷格,你是对的;我的意思是说“你必须使用 UIViewController”。 Kailoa 投票率最高的答案以更清晰的方式表达了我的观点。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-04
        相关资源
        最近更新 更多