【问题标题】:Ios MFMailComposeViewController not displaingIos MFMailComposeViewController 不显示
【发布时间】:2013-06-20 21:00:44
【问题描述】:

我正在尝试编写 rhodes 原生扩展扩展来打开 iOS MailComposer。 现在代码“工作”,但 MFMailComposeViewController 不显示,xcode 显示警告:

Attempt to present <MFMailComposeViewController: 0x12b60840> on <Iosmailto: 0xc1898d0> whose view is not in the window hierarchy!

我读到 UIViewControllers 必须在层次结构中,但我无法从 Rhodes ruby​​ 代码或 clear-C 扩展包装器中提供它。 现在我正在尝试使用 [UIApplication sharedApplication].keyWindow.rootViewController 中的 MFMailComposeController 来展示我的 UIViewController,但邮件编辑器尚未显示。 p>

来自 iosmailto.h 的接口:

@interface Iosmailto : UIViewController<MFMailComposeViewControllerDelegate>
{
}
    @property(nonatomic, assign) id delegate;
    //-(IBAction)send_mail:(id)sender;
@end

从 iosmailto.m 实现:

@implementation Iosmailto

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self send_mail];
    }

    - (void)send_mail {
        if ([MFMailComposeViewController canSendMail]) {

            MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
            composer.mailComposeDelegate = self;
            [composer setSubject:[NSString stringWithUTF8String:subj]];

            NSArray *recipients_array = [NSArray arrayWithObject:[NSString stringWithUTF8String:recipients]];

            [composer setToRecipients:recipients_array];

            NSString *composerBody = [NSString stringWithUTF8String:message];
            [composer setMessageBody:composerBody isHTML:NO];

            [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
            [self presentModalViewController:composer animated:YES];

            [composer release];
        } else {

            NSLog(@"Device is unable to send email in its current state.");
        }
    }
    /* some unnecessary for this question methods */

@end

在 iosmailto.m 中定义的 C 函数从 Rhodes 调用:

// find root vc
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

// find topmost vc
while (topController.presentedViewController) {
    topController = topController.presentedViewController;
}

iosmailer = [[Iosmailto alloc] init];

iosmailer.delegate = topController;
[topController presentViewController:iosmailer animated:YES completion:nil];

此应用不适用于 AppStore。如果需要,欢迎使用 Hack。

更新 Iosmailto 的初始化:

- (id)init
{
   self = [super initWithNibName:nil bundle:nil];
   return self;
}

更新 2 这段代码的简短版本(没有 UIViewController 包装器)是我的出发点。那也行不通。这有同样的警告,还有一个:

UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

while (topController.presentedViewController) {
    topController = topController.presentedViewController;
}
if ([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
    composer.mailComposeDelegate = (id<MFMailComposeViewControllerDelegate>)topController ;

    [composer setSubject:[NSString stringWithUTF8String:subj]];
    NSArray *recipients_array = [NSArray arrayWithObject:[NSString stringWithUTF8String:recipients]];
    [composer setToRecipients:recipients_array];
    NSString *composerBody = [NSString stringWithUTF8String:message];
    [composer setMessageBody:composerBody isHTML:NO];
    [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [topController presentModalViewController:composer animated:YES];

    [composer release];
} else {
}

【问题讨论】:

  • Iosmailto 的 init 方法是什么样的?您需要确保调用 initWithNib:(在 UIViewController 中定义,由您的子类继承)或确保您的自定义 init 方法调用 [super initWithNib:]
  • 使用 init 进行更新
  • 更新了没有包装器的代码
  • 使用 Update2 中的代码。在那种情况下,您的 topController 是否真的实现了 MFMailComposeViewControllerDelegate 协议?如果是这样,只需在分配之前将其转换为 id&lt;MFMailComposeViewControllerDelegate&gt;
  • 如果它没有实现协议,那么尝试不分配委托。

标签: iphone ios uiviewcontroller mfmailcomposeviewcontroller rhomobile


【解决方案1】:

使用此代码呈现视图控制器

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:composer
                                                                                                     animated:YES
                                                                                                   completion:nil];

【讨论】:

    【解决方案2】:

    好的,我认为问题在于您的 UIViewController 的视图属性中没有任何内容,因为您没有使用 .xib 文件对其进行初始化。 See this question 以编程方式创建 UIView 并将其分配给 UIViewController 的 view 属性。我认为这是正确的方法。查看他们的-(void)loadView 方法的实现。

    您的用例也有点奇怪,因为看起来您的 Iosmailto UIViewController 没有任何内容,而您只是将它用作 MFMailComposeViewController 的包装器——您可以考虑重新实现您的 C 方法以直接创建一个 MFMailComposeViewController,而不需要这个不必要的 UIViewController 的间接层,它本身不显示任何内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 2014-04-21
      相关资源
      最近更新 更多