我有这段代码:

- (IBAction)registerAction:(id)sender {
 [NSThread detachNewThreadSelector:@selector(registerThread) toTarget:self withObject:nil];
}

- (void)registerThread {
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

 MyDelegate *delegate = (MyDelegate *)[[UIApplication sharedApplication] delegate];


 NSInteger locationID = [delegate.api checkCoordinate:[NSString stringWithFormat:@"%f,%f",
             location.coordinate.latitude, location.coordinate.longitude]];

 NSNumber *status = [api registerWithUsername:usernameField.text
          password:passwordField.text email:emailField.text andLocation:locationID];

 [self performSelectorOnMainThread:@selector(registrationDoneWithStatus:) withObject:[NSNumber numberWithInt:1] waitUntilDone:NO];
    [pool release];
}

它工作正常,但有时会出现此错误:
void _WebThreadLockFromAnyThread(bool), 0x6157e30: Obtaining the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread.

似乎只有使用委托才会出现此错误,而且我不知道如何解决。

提前致谢 :)

最佳答案

我最近遇到了同样的问题。

可能会有一些 Activity 视图(例如UITextFieldUITextView)。在访问resignFirstResponder之前,先尝试delegate这些视图

关于iphone - 从线程调用委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4833782/

10-10 08:19