【发布时间】: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<MFMailComposeViewControllerDelegate>。 -
如果它没有实现协议,那么尝试不分配委托。
标签: iphone ios uiviewcontroller mfmailcomposeviewcontroller rhomobile