ios - 以编程方式加载 View

标签 ios uiviewcontroller segue

我的项目中有三个 View Controller 。我们称它们为 view1、view2 和 view3。 View2 使用 interface builder 的 segue 从 View1 加载。但是 view3 需要以编程方式从 view2 加载,因为 view2 向 REST 服务器发出 RESTful 请求并且该请求是异步的。我有一个单独的类来处理 REST 请求。从 REST 服务器获得返回值后,我调用 view2 的 loadResultViewController 方法。但是,我收到“libc++abi.dylib:以 NSException 类型的未捕获异常终止”错误。我该如何解决这个问题?

这是我的 loadResultViewController 方法

func loadResultViewController(summary: String) {
    let resultViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ResultView") as! ResultViewController
    self.presentViewController(resultViewController, animated: true, completion: nil)
    resultViewController.summary.text = summary

} 

编辑:

这是完整的错误信息。

2015-09-14 14:01:13.645 My Strategic[5307:791826] * Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /SourceCache/UIKit_Sim/UIKit-3347.44.2/Keyboard/UIKeyboardTaskQueue.m:374 2015-09-14 14:01:13.678 My Strategic[5307:791826] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.' *** First throw call stack: ( 0 CoreFoundation 0x000000010345dc65 exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000104fc8bb7 objc_exception_throw + 45 2 CoreFoundation 0x000000010345daca +[NSException raise:format:arguments:] + 106 3 Foundation 0x00000001038fa98f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195 4 UIKit 0x00000001044547d6 -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 151 5 UIKit 0x0000000103ef5912 -[UIKeyboardImpl setDelegate:force:] + 473 6 UIKit 0x00000001041a04ad -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 1002 7 UIKit 0x00000001041a8834 -[UIPeripheralHost(UIKitInternal) _preserveInputViewsWithId:animated:reset:] + 504 8 UIKit 0x0000000103e384f1 -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 623 9 UIKit 0x0000000103e3976e -[UIViewController _presentViewController:withAnimationController:completion:] + 3079 10 UIKit 0x0000000103e3b6c1 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 132 11 UIKit 0x0000000103e3b5e5 -[UIViewController presentViewController:animated:completion:] + 229 12 My Strategic 0x0000000102dbfc26 _TFC14My_Strategic22PostCodeViewController24loadResultViewControllerfS0_FSST_ + 870 13 My Strategic 0x0000000102dc0cae _TTWC14My_Strategic22PostCodeViewControllerS_12RestDelegateS_FS1_24loadResultViewControllerUS1___fQPS1_FSST_ + 94 14 My Strategic 0x0000000102dcfcf0 _TFFC14My_Strategic4Rest9getRidingFS0_FSST_U_FTGSQCSo13NSURLResponse_GSQCSo6NSData_GSQCSo7NSError__T_ + 2960 15 My Strategic 0x0000000102dcfe4a _TTRXFo_oGSQCSo13NSURLResponse_oGSQCSo6NSData_oGSQCSo7NSError__dT__XFdCb_dGSQS__dGSQS0__dGSQS1___dT + 90 16 CFNetwork 0x000000010566d8c5 67+[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]_block_invoke_2 + 155 17 Foundation 0x000000010391e57f __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK + 7 18 Foundation 0x000000010385f0b2 -[NSBlockOperation main] + 98 19 Foundation 0x0000000103841774 -[__NSOperationInternal _start:] + 645 20 Foundation 0x0000000103841383 __NSOQSchedule_f + 184 21 libdispatch.dylib 0x0000000106ac2614 _dispatch_client_callout + 8 22 libdispatch.dylib 0x0000000106aa96a7 _dispatch_queue_drain + 2176 23 libdispatch.dylib 0x0000000106aa8cc0 _dispatch_queue_invoke + 235 24 libdispatch.dylib 0x0000000106aac3b9 _dispatch_root_queue_drain + 1359 25 libdispatch.dylib 0x0000000106aadb17 _dispatch_worker_thread3 + 111 26 libsystem_pthread.dylib 0x0000000106e2f637 _pthread_wqthread + 729 27 libsystem_pthread.dylib 0x0000000106e2d40d start_wqthread + 13 ) libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

这是错误消息的相关部分:

reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'

您在后台线程上发出 REST 请求,然后在获得结果时,在同一线程上执行转换。所有 UI 代码通常应在主线程上运行。

尝试将代码包装在方法体中:

dispatch_async(dispatch_get_main_queue(),{
  // your code here
})

关于ios - 以编程方式加载 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32571262/

相关文章:

ios - 依靠 NSUbiquitousKeyValueStore 来存储基本数据是否安全?

iphone - UIDocumentInteractionController - 我们自己的应用出现在列表中

ios - 有没有办法知道 iOS 的共享模型何时会出现或消失?

iphone - ViewController 和绘制矩形

Swift:UICollectionView + Custom Segue 会出现问题

swift - UISwipeGestureRecognizer.Direction.down

ios - 在 swift 2.0 中过滤数组中的重复项并从另一个数组中删除数组元素

ios - 自定义 UIStoryboardSegue 中的 View 之间不显示过渡

ios - 在纵向状态下强制旋转 UIViewController

ios - 你能在 Storyboard中为自己定义一个 Segue 吗?