警告:不应从辅助线程调用UIKit [英] Warning: UIKit should not be called from a secondary thread

查看:116
本文介绍了警告:不应从辅助线程调用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"];

这篇关于警告:不应从辅助线程调用UIKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆