iOS 7 - UITabBarController selectedIndex 崩溃
Posted
技术标签:
【中文标题】iOS 7 - UITabBarController selectedIndex 崩溃【英文标题】:iOS7 - UITabBarController seletedIndex crash 【发布时间】:2013-10-03 13:28:57 【问题描述】:我正在开发基于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
【问题讨论】:
我也尝试了 malloc_info 命令,但没有成功! 能否包含堆栈跟踪或至少包含崩溃消息? 我已经包含了崩溃日志。 @SandeepM 我遇到了同样的问题。你找到解决办法了吗? @Afnan,请看我的回答。 【参考方案1】:你已经解决了这个问题。原因是视图控制器被释放但searchbar.delegate
没有设置nil
。所以你可以在视图控制器的dealloc
中设置searchBar.delegate = nil
。
【讨论】:
应该被接受的答案。它明确地描述了原因以及如何解决它!【参考方案2】:在 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;
【讨论】:
以上是关于iOS 7 - UITabBarController selectedIndex 崩溃的主要内容,如果未能解决你的问题,请参考以下文章
当 UIViewController 模态显示时,UINavigationController(在 UITabBarController 选项卡内)丢失堆栈
TabbarController 子控制器总是返回 nil - Swift 4