Iphone 为什么我的代码会因UIWebView异常而崩溃?
我的应用程序崩溃,输出如下:Iphone 为什么我的代码会因UIWebView异常而崩溃?,iphone,ios,ipad,Iphone,Ios,Ipad,我的应用程序崩溃,输出如下: 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 WebThreadLock 2 -[UIWebDocumentView(UIWebDocumentViewTextS
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 WebThreadLock
2 -[UIWebDocumentView(UIWebDocumentViewTextSelecting) selectionBaseWritingDirection]
3 -[UITextField _resignFirstResponder]
4 -[UIResponder _finishResignFirstResponder]
5 -[UIResponder resignFirstResponder]
6 -[UITextField resignFirstResponder]
7 -[UIView(UITextField) endEditing:]
8 -[UIWindowController _prepareKeyboardForTransition:fromView:]
9 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:]
10 -[UIViewController presentViewController:withTransition:completion:]
11 -[UIViewController presentViewController:animated:completion:]
12 -[UIViewController presentModalViewController:animated:]
13 -[studentlogViewController parse]
14 -[NSThread main]
15 __NSThread__main__
16 _pthread_start
17 thread_start
有人能告诉我吗,我疯了,我不知道这里发生了什么事你在后台解析一些数据(这很好),然后从后台线程更新UI,这是错误的。您必须从主线程更新UI:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[studentLogViewController parse];
dispatch_async(dispatch_get_main_queue(), ^{
// now present the new view controller
});
});
如果要执行任何UI操作,必须在主线程上执行 只需将编码粘贴到以下语法中
dispatch_sync(dispatch_get_main_queue(), ^{
//Your code goes here
});
[self performSelectorOnMainThread:@selector(yourmethod:)withObject:obj waitUntilDone:YES]
或者,您可以使用以下语法调用您的方法
dispatch_sync(dispatch_get_main_queue(), ^{
//Your code goes here
});
[self performSelectorOnMainThread:@selector(yourmethod:)withObject:obj waitUntilDone:YES]
很明显,您正在后台线程中使用webview。我在课堂上根本没有使用webview。我正在解析xml数据并在文本上显示。请看这里:有关类似问题的答案。您可能需要使用GCD或其他形式的回调来避免从后台线程调用
UIKit
方法hi-rich:这个dd_invokeonmainthread怎么样?我猜这是在iOS 6.1或更早版本上发生的。在此期间,UITextField在内部使用webview处理属性字符串布局。这就是为什么您可能会看到UIWebView异常,即使您没有直接使用它