在 IOS 中调用另一个屏幕时出现异常

Exception while calling another screen in IOS

我是 IOS 开发新手。在 Main.storyoard 中,我添加了一个新的 viewcontroller,将其分配给一个新的 class LoginViewController 并提供了一个故事板 ID loginview。 在 LoginViewController class 中,成功登录后,我尝试使用以下代码调用默认的 viewcontroller 故事板 ID webview.

NSString *storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"webview"];
[self presentViewController:vc animated:YES completion:nil];

在调试期间我遇到了以下异常

2016-05-04 18:37:11.020 gcmexample[2690:86862] bool _WebTryThreadLock(bool), 0x7fa5d3f3d840: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now... 1 0x11328d34b WebThreadLock 2 0x10e5547dd -[UIWebView _webViewCommonInitWithWebView:scalesPageToFit:] 3 0x10e555179 -[UIWebView initWithCoder:] 4 0x10e7b5822 UINibDecoderDecodeObjectForValue 5 0x10e7b5558 -[UINibDecoder decodeObjectForKey:] 6 0x10e5e1483 -[UIRuntimeConnection initWithCoder:] 7 0x10e7b5822 UINibDecoderDecodeObjectForValue 8
0x10e7b59e3 UINibDecoderDecodeObjectForValue 9 0x10e7b5558 -[UINibDecoder decodeObjectForKey:] 10 0x10e5e06c3 -[UINib instantiateWithOwner:options:] 11 0x10e3b9eea -[UIViewController _loadViewFromNibNamed:bundle:] 12 0x10e3ba816 -[UIViewController loadView] 13 0x10e3bab74 -[UIViewController loadViewIfRequired] 14 0x10e3bb2e7 -[UIViewController view] 15 0x10eb65f87 -[_UIFullscreenPresentationController _setPresentedViewController:] 16 0x10e38af62 -[UIPresentationController initWithPresentedViewController:presentingViewController:] 17 0x10e3cdc8c -[UIViewController _presentViewController:withAnimationController:completion:] 18 0x10e3d0f2c -[UIViewController _performCoordinatedPresentOrDismiss:animated:] 19 0x10e3d0a3b -[UIViewController presentViewController:animated:completion:] 20 0x10d065eed 34-[LoginViewController btnSign_in:]_block_invoke 21 0x10fd3f6b5 __75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke 22 0x10fd51a02 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke 23 0x10d1dc304 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK 24 0x10d118035 -[NSBlockOperation main] 25 0x10d0faf8a -[__NSOperationInternal _start:] 26 0x10d0fab9b __NSOQSchedule_f 27 0x11057f49b _dispatch_client_callout 28 0x1105658ec _dispatch_queue_drain 29 0x110564e0d _dispatch_queue_invoke 30 0x110567a56 _dispatch_root_queue_drain 31 0x1105674c5 _dispatch_worker_thread3

有谁知道这里的错误是什么?请帮忙。

尝试使用以下代码代替您的代码。问题似乎是您正在从为登录服务创建的不同胎面执行 UI 操作。但建议 UI 操作应该在主线程上执行

dispatch_async(dispatch_get_main_queue(), ^{
NSString *storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"webview"];
[self presentViewController:vc animated:YES completion:nil];
});

您正在辅助线程上执行当前操作 main thread。所有UI操作必须在Main Thread上执行,否则不会生效。

要在主线程上执行某些操作,请使用以下在主线程上执行的 dispatch_async 块。

dispatch_async(dispatch_get_main_queue(), ^{
    NSString *storyboardName = @"Main";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"webview"];
    [self presentViewController:vc animated:YES completion:nil];
});

试试这个

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyNetworkStoryboard" bundle:nil];
MyNetworkTableViewController *myVC = (MyNetworkTableViewController *)[storyboard instantiateViewControllerWithIdentifier:@"networkController"];
UIViewController *frontViewController = myVC;
[self.navigationController pushViewController:frontViewController animated:YES];