![](/img/trans.png)
[英]EXC_BAD_ACCESS error ONLY in iPad, NOT in iPhone & Simulator
[英]Error in simulator for iPad but not for iPhone
我有一個segue,我喜歡這樣:
[self performSegueWithIdentifier:@"PlanToBusiness" sender:self];
它適用於iPhone而不適用於iPad。 我所做的是遠程調用服務器,當服務器響應正確返回時,我嘗試將用戶帶到具有該segue的新頁面。
它適用於iPhone模擬器,但不適用於iPad(兩者的識別字符串相同),並且在iPad上它會因此錯誤而崩潰:
bool _WebTryThreadLock(bool), 0x68f9cb0: 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 -[UITextRangeImpl isEmpty]
3 -[UITextRange(UITextSelectionAdditions) _isCaret]
4 -[UITextSelectionView setCaretBlinks:]
5 -[UIKeyboardImpl setCaretBlinks:]
6 -[UIKeyboardImpl setDelegate:force:]
7 -[UIKeyboardImpl setDelegate:]
8 -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:]
9 -[UINavigationController navigationTransitionView:didStartTransition:]
10 -[UINavigationTransitionView transition:fromView:toView:]
11 -[UINavigationTransitionView transition:toView:]
12 -[UINavigationController _startTransition:fromViewController:toViewController:]
13 -[UINavigationController _startDeferredTransitionIfNeeded]
14 -[UINavigationController pushViewController:transition:forceImmediate:]
15 -[UINavigationController pushViewController:animated:]
16 -[UIStoryboardPushSegue perform]
17 -[UIStoryboardSegueTemplate perform:]
18 -[UIViewController performSegueWithIdentifier:sender:]
19 __41-[PlanBusinessController submitBusiness:]_block_invoke_0
20 __block_global_0
21 -[NSBlockOperation main]
22 -[__NSOperationInternal start]
23 -[NSOperation start]
24 __block_global_6
25 _dispatch_call_block_and_release
26 _dispatch_worker_thread2
27 _pthread_wqthread
28 start_wqthread
我也不習慣讀取Obective-C堆棧跟蹤,因此我很難確定問題所在。
我是這樣做的:
- (IBAction)submitBusiness:(id)sender
{
// Make a url to send
NSURL *url = [NSURL URLWithString:full_encoded_url_string];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
// ***************
// TODO: ok I dont really understand what this is
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// **************
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
// I took out a lot of the logic code that is not needed
[self performSegueWithIdentifier:@"PlanToBusiness" sender:self];
}];
}
}
當觸摸UI的方法(例如執行segue時),確保從主線程調用該代碼非常重要。 最簡單的方法是制作一個這樣的方法:
- (void)performPlanToBusinessSegueOnMainThread
{
[self performSegueWithIdentifier:@"PlanToBusiness" sender:self];
}
然后,您可以在完成的塊中調用此方法,如下所示:
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
// I took out a lot of the logic code that is not needed
[self performSelectorOnMainThread:@selector(performPlanToBusinessSegueOnMainThread)
withObject:nil
waitUntilDone:YES];
}];
這有點迂回,但它應該修復你的異常。 如果我們可以在performSelectorOnMainThread
方法中調用performSegueWithIdentifier
會更好,但它有太多的參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.