以编程方式加载视图 [英] Loading view programmatically

查看:29
本文介绍了以编程方式加载视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have three view controllers in my project. Let's call them view1, view2 and view3. View2 is loaded from View1 using segue from interface builder. But view3 needs to be loaded programmatically from view2 since view2 makes RESTful request to REST server and that request is asynchronous. I have a separate class to handle REST request. Once I get the return value from REST server, I invoke loadResultViewController method of view2. However I'm getting "libc++abi.dylib: terminating with uncaught exception of type NSException" error. How do I resolve this issue?

这是我的 loadResultViewController 方法

This is my loadResultViewController method

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 strategy[5307:791826] * 断言失败 -[UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished],/SourceCache/UIKit_Sim/UIKit-3347.44.2/TaskKeyboard.m3742015-09-14 14:01:13.678 My Strategic[5307:791826] * 由于未捕获的异常NSInternalInconsistencyException"而终止应用程序,原因:-[UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished] 只能从主线程调用.*** 首先抛出调用堆栈:(0 CoreFoundation 0x000000010345dc65 exceptionPreprocess + 1651 libobjc.A.dylib 0x0000000104fc8bb7 objc_exception_throw + 452 CoreFoundation 0x000000010345daca +[NSException raise:format:arguments:] + 1063 基础 0x00000001038fa98f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 1954 UIKit 0x00000001044547d6 -[UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished] + 1515 UIKit 0x0000000103ef5912 -[UIKeyboardImpl setDelegate:force:] + 4736 UIKit 0x00000001041a04ad -[UIPeripheralHost(UIKitInternal)_reloadInputViewsForResponder:] + 10027 UIKit 0x00000001041a8834 -[UIPeripheralHost(UIKitInternal)_preserveInputViewsWithId:animated:reset:] + 5048 UIKit 0x0000000103e384f1 -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 6239 UIKit 0x0000000103e3976e -[UIViewController _presentViewController:withAnimationController:completion:] + 307910 UIKit 0x0000000103e3b6c1 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 13211 UIKit 0x0000000103e3b5e5 -[UIViewController presentViewController:animated:completion:] + 22912 我的战略 0x0000000102dbfc26 _TFC14My_Strategic22PostCodeViewController24loadResultViewControllerfS0_FSST_ + 87013 我的策略 0x0000000102dc0cae _TTWC14My_Strategic22PostCodeViewControllerS_12RestDelegateS_FS1_24loadResultViewControllerUS1___fQPS1_FSST_ + 9414 我的战略 0x0000000102dcfcf0 _TFFC14My_Strategic4Rest9getRidingFS0_FSST_U_FTGSQCSo13NSURLResponse_GSQCSo6NSData_GSQCSo7NSError__T_ + 296015 我的战略 0x0000000102dcfe4a _TTRXFo_oGSQCSo13NSURLResponse_oGSQCSo6NSData_oGSQCSo7NSError__dT__XFdCb_dGSQS__dGSQS0__dGSQS1___dT + 9016 CFNetwork 0x000000010566d8c5 67+[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]_block_invoke_2 + 15517 基金会 0x000000010391e57f __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK + 718 基金会 0x000000010385f0b2 -[NSBlockOperation main] + 9819 基金会 0x0000000103841774 -[__NSOperationInternal _start:] + 64520 基金会 0x0000000103841383 __NSOQSchedule_f + 18421 libdispatch.dylib 0x0000000106ac2614 _dispatch_client_callout + 822 libdispatch.dylib 0x0000000106aa96a7 _dispatch_queue_drain + 217623 libdispatch.dylib 0x0000000106aa8cc0 _dispatch_queue_invoke + 23524 libdispatch.dylib 0x0000000106aac3b9 _dispatch_root_queue_drain + 135925 libdispatch.dylib 0x0000000106aadb17 _dispatch_worker_thread3 + 11126 libsystem_pthread.dylib 0x0000000106e2f637 _pthread_wqthread + 72927 libsystem_pthread.dylib 0x0000000106e2d40d start_wqthread + 13)libc++abi.dylib:以未捕获的 NSException 类型异常终止

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

推荐答案

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

This is the relevant part of the error message:

原因:'-[UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished] 只能从主线程调用.'

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

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

You're making the REST request on a background thread and then when you get the result, you perform the transition on the same thread. All UI code should generally run on the main thread.

尝试将方法体中的代码包装成这样:

Try wrapping the code in the method body in this:

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

这篇关于以编程方式加载视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆