以编程方式加载视图

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

这是我的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我的战略[5307:791826] *声明失败 – [UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished],/SourceCache/UIKit_Sim/UIKit-3347.44.2/Keyboard/UIKeyboardTaskQueue.m:374 2015-09-14 14:01:13.678我的策略[5307:791826] *由于未捕获的exception'NSInternalInconsistencyException',原因:' – [UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished]只能从主线程中调用。 ***第一次抛出调用堆栈:(0 CoreFoundation 0x000000010345dc65 exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000104fc8bb7 objc_exception_throw + 45 2 CoreFoundation 0x000000010345daca + [NSException raise:format:arguments:] + 106 3基础0x00000001038fa98f – [NSAssertionHandler handleFailureInMethod:object:文件:lineNumber:description:] + 195 UIKit 0x00000001044547d6 – [UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished] + 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:completio N:] + 3079 10的UIKit 0x0000000103e3b6c1 __62- [UIViewController中presentViewController:animation:完成:] _ block_invoke + 132 11的UIKit 0x0000000103e3b5e5 – [UIViewController中presentViewController:animation:完成:] + 229 12我的战略0x0000000102dbfc26 _TFC14My_Strategic22PostCodeViewController24loadResultViewControllerfS0_FSST_ + 870 13我的战略0x0000000102dc0cae _TTWC14My_Strategic22PostCodeViewControllerS_12RestDelegateS_FS1_24loadResultViewControllerUS1___fQPS1_FSST_ + 94 14我的战略0x0000000102dcfcf0 _TFFC14My_Strategic4Rest9getRidingFS0_FSST_U_FTGSQCSo13NSURLResponse_GSQCSo6NSData_GSQCSo7NSError__T_ + 2960 15我战略0x0000000102dcfe4a _TTRXFo_oGSQCSo13NSURLResponse_oGSQCSo6NSData_oGSQCSo7NSError__dT__XFdCb_dGSQS__dGSQS0__dGSQS1___dT + 90 16 CFNetwork的0x000000010566d8c5 67+ [NSURLConnection的sendAsynchronousRequest:队列:completionHandler:] _ block_invoke_2 + 155 17基金会0x000000010391e57f __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK + 7 18基金会0x00000 0010385f0b2 – [NSBlockOperation主] + 98 19基金会0x0000000103841774 – [__ NSOperationInternal _start:] + 645 20基金会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:与typesNSException的未捕获的exception终止

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

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

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

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

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

在build议中有一个小的拼写错误。expression式块应该像下面那样以'^'作为前缀

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