警告:不应从辅助线程调用UIKit

问题描述:

在我的iPhone应用程序中,我需要连接到Web服务器,因为这可能需要一些时间我使用这样的线程:

In my iPhone app I need to connect to a web server as this can take some time I'm using threads like this:

[NSThread detachNewThreadSelector:@selector(sendStuff) toTarget:self withObject:nil];

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

    //Need to get the string from the textField to send to server
    NSString *myString = self.textField.text;

    //Do some stuff here, connect to web server etc..

    [pool release];
}

在我使用self.textField的行上,我在控制台中收到警告说:
void _WebThreadLockFromAnyThread(bool),0x5d306b0:从主线程或Web线程以外的线程获取Web锁。 UIKit不应该从辅助线程调用。

On the row where I use self.textField I get a warning in console saying: void _WebThreadLockFromAnyThread(bool), 0x5d306b0: 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.

如何在不收到此错误的情况下使用textField?

How can I use the textField without getting this error?

这取决于你想用textField做什么。如果读取值是唯一的,你可以这样做:

It depends a little bit on what you want to do with the textField. If reading the value is the only thing, you can do:

[NSThread detachNewThreadSelector:@selector(sendStuff) toTarget:self withObject:self.textField.text];

- (void)sendStuff:(NSString*)myString {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    //Do someting with myString
    [pool release];
}

如果你想改变textField上的值,你可以:

If you want to change a value on the textField you could:

[self.textField performSelectorOnMainThread:@selector(setText:) withObject:@"new Text"];