简体   繁体   English

iPad模拟器出错,但iPhone出现错误

[英]Error in simulator for iPad but not for iPhone

I have a segue that I do like this: 我有一个segue,我喜欢这样:

[self performSegueWithIdentifier:@"PlanToBusiness" sender:self];

it works on the iPhone but not on the iPad. 它适用于iPhone而不适用于iPad。 What I do is make a remote call to a server, and when the server response comes back correctly, I try to take the user to the new page with that segue. 我所做的是远程调用服务器,当服务器响应正确返回时,我尝试将用户带到具有该segue的新页面。

It works on the iPhone simulator, but not the iPad (the identifying string is the same for both) and on the iPad it crashes with this error: 它适用于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

I am also not too used to reading Obective-C stack traces so I am having a hard time pinpointing what the problem is. 我也不习惯读取Obective-C堆栈跟踪,因此我很难确定问题所在。

Here is how I do this: 我是这样做的:

- (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];
         }];
    }
}

When methods that touch the UI, such when performing a segue, it is important to make sure you are calling that code from the main thread. 当触摸UI的方法(例如执行segue时),确保从主线程调用该代码非常重要。 The easiest way to do that is to make a method like this: 最简单的方法是制作一个这样的方法:

- (void)performPlanToBusinessSegueOnMainThread
{
    [self performSegueWithIdentifier:@"PlanToBusiness" sender:self];
}

Then, you can call this method in your finished block like this: 然后,您可以在完成的块中调用此方法,如下所示:

[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];
 }];

It is a little circuitous, but it should fix your exception. 这有点迂回,但它应该修复你的异常。 It would be better if we could just call performSegueWithIdentifier in the performSelectorOnMainThread method, but it has too many arguments. 如果我们可以在performSelectorOnMainThread方法中调用performSegueWithIdentifier会更好,但它有太多的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM