适用于 iOS 模拟器但不适用于 iPhone [英] Works on iOS Simulator but not on iPhone

查看:46
本文介绍了适用于 iOS 模拟器但不适用于 iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这行代码在 iOS Simulator 6.0 上运行良好,但是当我尝试在我的 iPhone(也运行 iOS6)上运行时崩溃了.

The line of code works fine on the iOS Simulator 6.0, but is crashing when I try to run it on my iPhone, also running iOS6.

[menuView addSubview:mvc.view];

为什么会发生这种情况,我该如何解决?

Why is this happening, and how can I fix it?

这是更完整的代码版本:

This is the more complete version of the code:

SDMenuViewController *mvc = [[SDMenuViewController alloc] init];
[menuView addSubview:mvc.view];

这就是它崩溃的原因:

2012-10-08 21:32:32.423 CrunchCalculator1-2[21019:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/EDD23933-CE20-4AFD-A2B1-CDD56AD658E8/CrunchCalculator1-2.app> (loaded)' with name 'SDNestedTableView''
*** First throw call stack:
(0x39cd03e7 0x35ece963 0x39cd0307 0x39ee0fd1 0x39ee05ff 0x39dd9031 0x39e0786d 0x39d63419 0xb20d9 0x39d63541 0x39da3d29 0x39d9fac5 0x39de1199 0xb17c5 0x39da4a71 0x39da45f5 0x39d9c803 0x39d44ce7 0x39d44775 0x39d441b7 0x31e145f7 0x31e14227 0x39ca53e7 0x39ca538b 0x39ca420f 0x39c1723d 0x39c170c9 0x39d9b43d 0x39d98289 0xb1523 0x3792fb20)
libc++abi.dylib: terminate called throwing an exception

谢谢!

推荐答案

我不太确定它在你的模拟器上是如何工作的(当我在我的模拟器上尝试时,我得到了你在原始问题中列出的崩溃).无论如何,您可以通过查看以下项目来修复它:

I'm not quite sure how it worked on your simulator (when I tried it on mine, I got the crash you list in your original question). Anyway, you can fix it by looking at the following items:

  • 主要问题是 NIB 未包含在捆绑包中.将其添加到项目目标的复制捆绑资源"中,例如:

  • 在查看复制捆绑资源"时,您还需要包含 SDSubCell.xibSDGroupCell.xib,并添加所有这些 PNG 文件也是如此.

  • While you're looking at your "Copy Bundle Resources", you'll also want to include SDSubCell.xib, SDGroupCell.xib, and add all of those PNG files, too.

顺便说一句,虽然它显然不会导致崩溃,但 SDNestedTableView NIB 中的文件所有者"基类指的是这个项目中任何地方都不存在的类.那不可能是好事.无论如何,您可能希望将其更改为 SDMenuViewControllerSDNestedTableViewController;

As an aside, while it doesn't apparently cause the crash, the "File Owner" base class in SDNestedTableView NIB refers to a class that doesn't exist anywhere in this project. That can't be good. Anyway, you probably want to change that to SDMenuViewController or SDNestedTableViewController;

这与您的崩溃有点无关,但是当我查看该项目时,我看到了一个令人担忧的结构:

It's a little unrelated to your crash, but as I look at the project, I see a worrying construct:

SDMenuViewController *mvc = [[SDMenuViewController alloc] initWithNibName:@"SDNestedTableView" bundle:nil];
[menuView addSubview:mvc.view];

您正在创建一个控制器,获取其视图,然后让视图控制器超出范围并被释放(如果您使用的是 ARC)或泄漏它(如果不是 ARC).

You're creating a controller, grabbing its view, and either letting the view controller fall out of scope and be released (if you were using ARC) or leaking it (if not ARC).

根据最初的问题,我并不确定您是否正在将 addSubview 作为过渡到新视图的一种方式(这确实是一种糟糕的做法),或者您是否在进行视图控制器包含.当我查看代码时,虽然您在代码中遗漏了一些调用,但您似乎正在执行后者.您可能想阅读 视图控制器控制.还要查看 WWDC 2011 session 102.

I wasn't entirely sure from the original question whether you were doing addSubview as a way of transitioning to a new view (which is really bad practice) or whether you were doing view controller containment. As I look at the code, it appears you're doing the latter, though you're missing a few calls in your code. You might want to read up on view controller containment. And also check out that WWDC 2011 session 102.

无论如何,上面带有视图控制器 alloc/init 和随后的 addSubview 的这两行代码将在您的非 ARC 中泄漏项目(如果您曾经使用 ARC,它会崩溃)并且您的视图层次结构与您的视图控制器层次结构不同步.我建议你可能想要:

Anyway, those two lines of code above with the view controller alloc/init and the subsequent addSubview will leak in your non-ARC project (and would crash it if you ever went to ARC) and your view hierarchy is out of sync with your view controller hierarchy. I'd suggest you might want:

SDMenuViewController *mvc = [[[SDMenuViewController alloc] initWithNibName:@"SDNestedTableView" bundle:nil] autorelease];
[self addChildViewController:mvc];
[mvc didMoveToParentViewController:self];
[menuView addSubview:mvc.view];

注意第一行的 autorelease.

视图控制器包含功能可能很强大,但您需要确保您执行一些基本的内务处理.

View controller containment can be powerful, but you want to make sure you do some of this basic housekeeping.

最后一次更新:

我注意到这段代码中有一些错误.首先,您在 item:setSubItem:forRowAtIndexPath 中使用 currentSection 将不起作用.您正在根据最后一个 expandingItem 进行设置.因此,如果在展开任一主要项目之前单击其中一个主要项目,程序将崩溃.可能最好的方法是完全消除 currentSection 变量,并在 item:setSubItem:forRowAtIndexPath 中使用 item.cellIndexPath.row 而不是变量 >currentSection.

I notice that there are some bugs that are in this code. First, your use of currentSection in item:setSubItem:forRowAtIndexPath won't work. You're setting that based upon the last expandingItem. So, if you click on one of the main items before expanding either one, the program will crash. Probably best is to eliminate the currentSection variable altogether and in item:setSubItem:forRowAtIndexPath, use item.cellIndexPath.row rather than your variable currentSection.

不幸的是,此修复导致了一个更严重的问题,SDNestedTable 类本身似乎存在 iOS 6 错误.如果您在 iOS 6 上运行它并展开所有项目,滚动到底部然后滚动回顶部,程序将崩溃,因为 SDGroupItem * 的 cellIndexPath 属性item:setSubItem:forRowAtIndexPath 返回的 item 可以被释放!如果您在 iOS 6 中打开僵尸,您将看到 cellIndexPath 已发布.我去下载了 原始版本 并在那里看到了同样的问题.问题似乎是 SDGroupCell 中的 cellIndexPath 被定义为 assign 属性(这意味着如果 iOS 确定它不再需要 indexPath 它是为了自己的目的而创建的,即使 SDGroupCell 维护一个对这个释放对象的 assign 引用,它也会被释放.只需将 SDGroupCellcellIndexPath 属性从 assign 更改为 retain,这个 iOS 6 错误就会消失.我已将此问题告知 SDNestedTable 的开发者,但对 retain 的更改将解决 iOS 6 中代码崩溃的问题.

Unfortunately, this fix leads to a more serious problem, there appears to be an iOS 6 bug in the SDNestedTable class, itself. If you run this on iOS 6 and you expand your all of your items, scroll to the bottom and then scroll back to the top, the program will crash because the cellIndexPath property of the SDGroupItem *item returned by item:setSubItem:forRowAtIndexPath can be deallocated! If you turn on zombies in iOS 6, you'll see cellIndexPath has been released. I went and downloaded the original version and see the same problem there. The problem appears to be that cellIndexPath in SDGroupCell is defined as an assign property (which means that if iOS determines it no longer needed the indexPath it created for its own purposes, it will be released even though SDGroupCell maintains an assign reference to this released object). Just change the cellIndexPath property of SDGroupCell from assign to retain, and this iOS 6 bug goes away. I've informed the developer of SDNestedTable of this issue, but this change to retain will fix the problem of the code crashing in iOS 6.

最好的祝福.

这篇关于适用于 iOS 模拟器但不适用于 iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆