我用 NSOperation 和 NSOperationQueue 建立了一个测试项目。只有一个文本框:@property (weak) IBOutlet NSTextField *textbox;
在后台执行此操作:
- (void)main
{
NSURL *url = [NSURL URLWithString:@"http://google.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(@"MainThread: %@", ([NSThread isMainThread] ? @"YES" : @"NO"));
[[AppDelegate sharedManager] performSelectorOnMainThread:@selector(pageLoaded:)
withObject:response
waitUntilDone:NO];
}
以及在主线程上调用的选择器:
- (void)pageLoaded:(NSString*)document
{
[textbox setStringValue:document]; // does nothing
NSLog(@"Textbox: %@", textbox); // returns nil
}
为什么文本框返回零?