2016-11-26 140 views
0

我想presentViewController但崩溃与下面的堆栈跟踪。 有人可以检查和帮助。呈现视图控制器时崩溃

代码以presentViewController

func moveToChatView(){ 
SwiftSpinner.show(Strings.loading) 

    let destViewController:GroupChatViewController = UIStoryboard(name:  "GroupChat", bundle: nil).instantiateViewControllerWithIdentifier("GroupChatViewController") as! GroupChatViewController 
destViewController.currentRiderRideID = self.riderRideId! 
if NSThread.isMainThread() == true{ 
self.presentViewController(destViewController, animated: true, completion: nil) 
}else{ 
    dispatch_sync(dispatch_get_main_queue()){ 
    self.presentViewController(destViewController, animated: true, completion: nil) 
    } 
} 
} 

致命异常:NSRangeException 0的CoreFoundation 0x182b482d8 exceptionPreprocess 1 libobjc.A.dylib 0x1948140e4 objc_exception_throw 2的CoreFoundation 0x182a2f4c0 CFStringConvertNSStringEncodingToEncoding 3的UIKit 0x1879e01b4 - [UINib instantiateWithOwner:选项:] 4 UIKit 0x1878dc318 - [UIViewController _loadViewFromNibNamed:bundle:] 5 UIKit 0x1875c09bc - [UIViewController loadVi ewIfRequired] 6的UIKit 0x1875c0928 - [UIViewController的视图] 7的UIKit 0x187cb618c - [_ UIFullscreenPresentationController _setPresentedViewController:] 8的UIKit 0x1878c60dc - [UIPresentationController initWithPresentedViewController:presentingViewController:] 9的UIKit 0x1878e2378 - [UIViewController中_presentViewController:withAnimationController:完成:] 10的UIKit 0x1808e8c8 __62- [UIViewController presentViewController:animated:completion:] _ block_invoke 11 UIKit 0x1876ae0ec - [UIViewController presentViewController:animated:completion:] 12 Quickride 0x100451968部分适用于LiveRideMapViewController。(moveToChatView() - >())。(closure#1 )(LiveRideMapViewController.swift:1868) 13 libdispatch.dylib 0x194e91954 _dispatch_client_callout 14 libdispatch.dylib 0x194e9f590 _dispatch_barrier_sync_f_slow_invoke 15 libdispatch.dylib 0x194e91954 _dispatch_client_callout 16 libdispatch.dylib 0x194e9620c _dispatch_main_queue_callback_4CF 17的CoreFoundation 0x182aff7f8 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE 18的CoreFoundation 0x182afd8a0 __CFRunLoopRun 19的CoreFoundation 0x182a292d4 CFRunLoopRunSpecific 20 GraphicsServices 0x18c47f6fc GSEventRunModal 21的UIKit 0x187626f40 UIApplicationMain 22 Quickride 0x100259a70 main(AppDelegate.swift:23) 23 libdyld.dylib 0x194ebea08 start

+0

是你确定'GroupChatViewController'在你的故事板'GroupChat'里面,而不在'xib'里面? –

+1

您可以放下一个断点并告诉我们哪一行特别引起异常? – Erik

+0

是的,当试图呈现GroupChatViewController我验证线程是否在主线程中,在这个流程中,它在后台线程,所以它去了别的块,一旦进入dispatch_sync(dispatch_get_main_queue())self.presentViewController(destViewController,动画:真,完成:无) }它崩溃了,是因为当前视图控制器不可用 –

回答

0

按规定不使用DISPATCH-同步,而是使用DISPATCH-异步,你没有,如果你检查主线程上,如果它是一个UI操作,有一个机会,这将是上一回线只是做DISPATCH-异步到主队列,这将在主队列中的下一个运行循环执行UI操作和UI将“平滑”,试试这个:

func moveToChatView(){ 
    SwiftSpinner.show(Strings.loading) 

    let destViewController:GroupChatViewController = UIStoryboard(name:"GroupChat", bundle: nil).instantiateViewControllerWithIdentifier("GroupChatViewController") as! GroupChatViewController 
    destViewController.currentRiderRideID = self.riderRideId! 
    dispatch_async(dispatch_get_main_queue()){ 
     self.presentViewController(destViewController, animated: true,completion:nil) 
    } 
} 
相关问题