1

我正在开发一个TabBar基于应用程序。在某些时候我正在做:

if([[[appDelegate.tabBarController viewControllers] objectAtIndex:1] isKindOfClass:[UINavigationController class]]) {
    UINavigationController *navController = [[appDelegate.tabBarController viewControllers] objectAtIndex:1];
    [navController popToRootViewControllerAnimated:NO];
}
appDelegate.tabBarController.selectedIndex = 1; // CRASH HERE

iOS6它工作正常,但是当我使用 XCode5/iOS7 构建应用程序时,应用程序会随机崩溃。请帮帮我。

谢谢!

崩溃日志:

* thread #1: tid = 0x4b606, 0x0557f811 CoreFoundation`___forwarding___ + 769, queue = 'com.apple.main-thread, stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
frame #0: 0x0557f811 CoreFoundation`___forwarding___ + 769
frame #1: 0x0557f4ee CoreFoundation`_CF_forwarding_prep_0 + 14
frame #2: 0x01128d6a UIKit`-[UISearchBar _didMoveFromWindow:toWindow:] + 168
frame #3: 0x00eca847 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 761
frame #4: 0x00eca847 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 761
frame #5: 0x00eca847 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 761
frame #6: 0x00ec2070 UIKit`__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 162
frame #7: 0x00ec1ef8 UIKit`-[UIView(Hierarchy) _postMovedFromSuperview:] + 260
frame #8: 0x00ecd031 UIKit`-[UIView(Internal) _addSubview:positioned:relativeTo:] + 1847
frame #9: 0x00ec0521 UIKit`-[UIView(Hierarchy) addSubview:] + 56
frame #10: 0x00f5b1f8 UIKit`-[UITransitionView transition:fromView:toView:removeFromView:] + 1205
frame #11: 0x00f5ad3b UIKit`-[UITransitionView transition:fromView:toView:] + 62
frame #12: 0x00f5aadb UIKit`-[UITransitionView transition:toView:] + 123
frame #13: 0x00fafe45 UIKit`-[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 1438
frame #14: 0x00faf262 UIKit`-[UITabBarController transitionFromViewController:toViewController:] + 63
frame #15: 0x00fab64b UIKit`-[UITabBarController _setSelectedViewController:] + 279
frame #16: 0x00fab470 UIKit`-[UITabBarController setSelectedIndex:] + 261
frame #17: 0x00014e6d TestApp`-[HomeViewController btn_TopRatedClicked:](self=0x0d813830, _cmd=0x0023559b, sender=0x0dbceb80) + 1533 at HomeViewController.m:707
frame #18: 0x02311874 libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 77
frame #19: 0x00e66c8c UIKit`-[UIApplication sendAction:to:from:forEvent:] + 108
frame #20: 0x00e66c18 UIKit`-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
frame #21: 0x00f5e6d9 UIKit`-[UIControl sendAction:to:forEvent:] + 66
frame #22: 0x00f5ea9c UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 577
frame #23: 0x00f5dd4b UIKit`-[UIControl touchesEnded:withEvent:] + 641
frame #24: 0x00ea40cd UIKit`-[UIWindow _sendTouchesForEvent:] + 852
frame #25: 0x00ea4d34 UIKit`-[UIWindow sendEvent:] + 1232
frame #26: 0x00e78a36 UIKit`-[UIApplication sendEvent:] + 242
frame #27: 0x00e62d9f UIKit`_UIApplicationHandleEventQueue + 11421
frame #28: 0x055188af CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
frame #29: 0x0551823b CoreFoundation`__CFRunLoopDoSources0 + 235
frame #30: 0x0553530e CoreFoundation`__CFRunLoopRun + 910
frame #31: 0x05534b33 CoreFoundation`CFRunLoopRunSpecific + 467
frame #32: 0x0553494b CoreFoundation`CFRunLoopRunInMode + 123
frame #33: 0x02b739d7 GraphicsServices`GSEventRunModal + 192
frame #34: 0x02b737fe GraphicsServices`GSEventRun + 104
frame #35: 0x00e6594b UIKit`UIApplicationMain + 1225
frame #36: 0x0000213d TestApp`main(argc=1, argv=0xbfffed08) + 141 at main.m:16

在 HomeViewController.m:707 我发现了以下语句:

appDelegate.tabBarController.selectedIndex = 1; // CRASH HERE
4

2 回答 2

3

你已经解决了这个问题。原因是视图控制器已释放但未searchbar.delegate设置nil。所以你可以设置视图控制器searchBar.delegate = nildealloc

于 2014-03-27T08:09:30.290 回答
0

在 iOS7 中,导航控制器堆栈崩溃了,所以我做了以下操作:

NSMutableArray *allControllersArray = [[appDelegate.tabBarController viewControllers] mutableCopy];
SearchViewController *viewController2 = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil];
UINavigationController *search=[[UINavigationController alloc] initWithRootViewController:viewController2]; 
[allControllersArray replaceObjectAtIndex:1 withObject:search];

appDelegate.tabBarController.viewControllers = allControllersArray;

在那之后:

appDelegate.tabBarController.selectedIndex = 1;
于 2013-11-22T11:16:37.743 回答