我在标签栏应用程序中遇到了一个奇怪的似乎是过度发布的问题。对于这个问题描述的复杂性,我深表歉意。希望一些示例代码会有所帮助。
我将我的应用程序委托(delegate) MyAppDelegate
设置为 UITabBarControllerDelegate
:
- (BOOL)tabBarController:(UITabBarController *)tabBarControllerIn shouldSelectViewController:(UIViewController *)viewController {
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarControllerIn didSelectViewController {
if ([viewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navController = (UINavigationController *)viewController;
[navController popToRootViewControllerAnimated:NO];
}
}
标签栏 View 为 5 个标签中的每一个标签设置了一个 UINavigationController
。为选项卡 4 配置的 UINavigationController
中的 Root View Controller (称为 CrashingViewController
)派生自 UIViewController
并符合 UITableViewDataSource
和 UITableViewDelegate
协议(protocol)支持 UITableView
subview ,它只是一个 4 行表格,其中的每个单元格都允许用户导航到另一个 View 。在 -[UITableViewDelegate tableView:didSelectRowAtIndexPath:]
中:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ASubViewController1 *svc1;
ASubViewController2 *svc2; // this one inherits from UITableViewController;
ASubViewController3 *svc3;
ASubViewController4 *svc4;
switch (indexPath.row) {
case 1:
svc1 = [[ASubViewController1 alloc] init];
[self.navigationController pushViewController:svc1 animated:YES];
[svc1 release];
break;
case 2:
svc2 = [[ASubViewController2 alloc] init];
[self.navigationController pushViewController:svc2 animated:YES];
[svc2 release]; // if I comment this out, I avoid the problem described
break;
/* case 3 and 4 are exactly like the previous two */
default:
break;
}
}
发生的事情是这样的:如果用户选择标签栏的第 4 个标签,则显示 CrashingViewController
的 View 。然后,用户点击其表格中的第二个单元格,这会将 ASubViewController2
的实例推送到导航堆栈中。到目前为止,一切都很好。现在,如果用户点击选项卡栏上的另一个选项卡,则会显示另一个 View Controller 的 View 。如果用户随后再次点击第 4 个选项卡,配置为该选项卡的 Root View Controller 的 UINavigationController
会弹出其所有项目,并且应该显示它的 Root View Controller 。正是在此期间,应用程序崩溃了,因为 UIKit 框架在 svc2
已经被释放后调用了 [ASubViewController2 respondsToSelector:]。
我遇到的困难是:为什么当用户导航到 ASubViewController2
而不是其他 ASubViewController*
时会发生崩溃秒?此外,如果我未能发布 svc2
,则不会发生崩溃 - 所以它肯定是在某处对 svc2
进行双重发布,我只是不知道发生了什么。 svc2
是方法局部变量,我在分配后立即释放它。 UITableViewController
在从导航 Controller 的堆栈中弹出时的行为是否与 UIViewController
不同?我还缺少其他东西吗?
我确信我只是遗漏了一些有据可查或简单明了的东西,但我已经研究了足够长的时间,以至于发现了一些盲点。
更新:
我有 NSZombiesEnabled。这是控制台中给出的消息:
*** -[ASubViewController2 respondsToSelector:]: message sent to deallocated instance 0xe345cd0
回溯:
#0 0x02a18fa7 in ___forwarding___ ()
#1 0x02a18e72 in __forwarding_prep_0___ ()
#2 0x003e69be in -[UITableView(UITableViewInternal) _spacingForExtraSeparators] ()
#3 0x003ede94 in -[UITableView(_UITableViewPrivate) _adjustExtraSeparators] ()
#4 0x003e6743 in -[UITableView layoutSubviews] ()
#5 0x027b9481 in -[CALayer layoutSublayers] ()
#6 0x027b91b1 in CALayerLayoutIfNeeded ()
#7 0x027b22e0 in CA::Context::commit_transaction ()
#8 0x027b2040 in CA::Transaction::commit ()
#9 0x027e2ebb in CA::Transaction::observer_callback ()
#10 0x02a88f4b in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#11 0x02a1db27 in __CFRunLoopDoObservers ()
#12 0x029e6ce7 in __CFRunLoopRun ()
#13 0x029e6350 in CFRunLoopRunSpecific ()
#14 0x029e6271 in CFRunLoopRunInMode ()
#15 0x0329200c in GSEventRunModal ()
#16 0x032920d1 in GSEventRun ()
#17 0x00380af2 in UIApplicationMain ()
#18 0x0000226a in main (argc=1, argv=0xbfffef64) at ~/main.m:16
更新 2:
ASubViewController2
的dealloc
方法在用户第二次点击tab 4 之后、僵尸消息输出之前在svc2
上被调用。我的代码都不在 dealloc 的代码路径中。
最佳答案
我遇到过类似的问题。似乎 View Controller 已经被释放,但它的 TableView 仍然调用被释放的 View Controller 的委托(delegate)。只需在 View Controller 的 dealloc 中添加以下或类似行:
self.tableView.delegate = nil; self.tableView.dataSource = nil;
对我有帮助!
关于iphone - 从 UINavigationController 堆栈弹出时,UITableViewController 是否有特殊行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3740502/