PresentModalViewController 使我的应用程序崩溃
我知道,这是最简单的事情之一。 但几天来我一直在努力反对这一点。 我过去已经做过很多次了,但由于某种原因,尝试呈现模式视图控制器只会使应用程序崩溃到黑屏。 控制台中没有报告任何内容。 我希望有人可能遇到过这个问题并提供一些建议。
此代码是从 UIViewController 类调用的:
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];
It's one of the simplest things to do, I know. But I've been banging my head against this for days. I've done it plenty of times in the past, but for some reason trying to present a modal view controller just crashes the app to a black screen. Nothing reported in the console or anything. I'm hoping someone might have had this problem and has some advice.
This code is called from a UIViewController class:
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如安德鲁在评论中指出的那样,您
在尝试推送视图控制器之前是否进行了检查? 如果此方法返回 NO,则 MFMailComposeViewController 的行为未明确定义(在模拟器上运行时也可能出现这种情况,尽管我不确定)。 从文档中:
您是否尝试过推送另一个视图控制器? 这也会使您的应用程序崩溃吗?
As Andrew has pointed out in his comment, do you check
before trying to push the view controller? The behavior of the
MFMailComposeViewController
is not well defined if this method returns NO (which may also well be the case when running on the simulator, though I'm not sure). From the documentation:Have you tried to push another view controller instead? Does this crash your app, too?
在尝试显示 MFMailComposeViewController 之前,您是否显示另一个模式视图控制器? 我遇到了同样的问题并找到了解决方法:
我知道这是一个丑陋的解决方法,但我没有找到更好的方法:(
Are you showing another modal view controller before trying to show MFMailComposeViewController? I had the same problem and found a workaround:
I know it's an ugly workaround, but I didn't found anything better :(
我不知道这有多相关,但在从另一个模态视图控制器返回到我的主视图控制器后,我在尝试呈现 MFMailComposeViewController 时遇到了可怕的问题。 它就是行不通。 我试图在启动邮件控制器之前关闭另一个模式视图控制器,并发现我的解决方案是不调用:
而是调用:
然后继续并显示邮件视图控制器。
这一改变使我的情况发生了巨大变化。 我怀疑这与 sgosha 遇到的问题有关。 只需关闭动画,而不是延迟(这可能只是等待动画完成)。 对我来说,看起来像是框架中的一个错误。
我也许应该进一步解释。 我的主视图控制器上有一个共享按钮,它会以模式方式弹出一个表格视图,以允许用户选择他们想要共享的内容。 从这里,如果他们点击“电子邮件”,他们会收到一个 UIActionSheet,让他们进一步决定要附加到电子邮件中的文件。
混合中的 UIActionSheet 可能会导致该问题。 模态视图控制器的实际解除是在我的主视图控制器中的委托方法中进行的,正是这个主视图控制器在解除模态表视图控制器后尝试启动邮件视图控制器。
I don't know how relevant this is, but I have been having horrible problems trying to present the MFMailComposeViewController after returning to my main view controller from ANOTHER modal view controller. It just would not work. I was trying to dismiss this other modal view controller before launching the mail controller, and found my solution to be to not call:
But to instead call:
Then go ahead and present the mail view controller.
That one change made all the difference in my case. I suspect this is connected to the problem sgosha was having. Just switch off the animation, rather than putting a delay in (which is probably just waiting until the animation has completed). Looks like a bug in the framework to me.
I should probably explain further. I have a share button on my main view controller, which modally pops up a table view to allow to user to choose what they want to share. From here, if they tap on Email, they get a UIActionSheet letting them further decide which files they wish to attach to their email.
It is possible the UIActionSheet in the mix is contributing to the problem. The actual dismissal of the modal view controllers is taking place in the delegate methods back in my main view controller, and it is this main view controller which tries to launch the mail view controller after dismissing the modal table view controller.
是的! 我做到了!我不敢相信,但我解决了问题!这与:
从(以及之上)其他一些打开的模态控制器打开 MFMailComposeViewController 作为模态
不幸的是,我不得不再次承认,在 iPhone 5 价格昂贵的时代,苹果仍然迫使开发人员使用旧的、有缺陷的、不方便的代码和组件! MFMailComposeViewController 就是一个很好的例子。
但是现在让我们来看看更令人愉快的事情。
我们有什么:
所以,我花了 5 个工作时间发现,代表似乎在某个时候以某种方式“释放”。 我可以假设,如果您将电子邮件控制器作为模态而不是其他模态控制器打开,则该(以前的模态)控制器正在通过垃圾收集器或其他方式进行清理,这样委托也将被清理(我不希望多花几个小时进行详细挖掘,所以我将这留给苹果的良心)。
无论如何,用两个词来说,我的解决方案是
将委托对象保存在具有“强”引用的地方。
就我而言,该委托的所有者是主视图控制器类(在我的情况下始终可用,因为大多数应用程序逻辑都在使用它)。 那也可以是 AppDelegate 实例。
看起来像:
坦白说,代码有点乱,但这只是一般想法。
我希望,你能更好地管理自己。
祝你好运,上帝保佑微软! ^^
Yes! I did it! I can not believe, but I solved the problem! That's related to:
Opening an MFMailComposeViewController as modal from (and over) some other opened modal controller
Unfortunately, I have to admit that, again, in times of expensive-like-a-Devil-iPhones5, Apple still forses developers to use old, buggy and not convenient code and components! The great example of that is MFMailComposeViewController.
But now let's follow to more pleasant things.
What do we have:
So, what my killed 5 working hours discovered is that seems the delegate is "releases" somehow somewhen. I can suppose that if you are opening the email controller as modal over some other modal controller, that (previously modal) controller is being cleaned by garbage collector or some other way and that way the delegate is being cleaned as well (I have no wish to kill several more hours for detailed digging, so I'd leave that to Apple's conscience).
Anyway, in two words, my solution was
to hold the delegate object somewhere with "strong" reference.
in my case, the owner of that delegate was main view controller class (which is always available in my case, as most of the app logic is working with it). That can be AppDelegate instance also.
It would look like:
Frankly saying, the code is kind of messy, but this is just general thoughts.
I hope, you'll manage your own much nicer.
Good luck and God bless Microsoft! ^^