2010-07-18 61 views
0

我在使用NSOpeation時遇到了一個奇怪的行爲。 我在調用一個函數(-createTagView),它創建一個UIButton然後將其添加到視圖。 由於某些原因,它不會添加它們。如果我從NSOperation之外調用該函數,那麼一切正常。NSOperation不會將子視圖添加到主視圖

任何想法?謝謝。

此我如何創建的NSOperation(一個的ViewController對象內)

> NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(createTagView:) object:data]; 
> [operationQueue addOperation:operation]; 
> [operation release]; 

這是調用的函數([標記視圖]爲的UIButton):

-(void) createTagView:(NSMutableArray *) data 
{ 
NSInteger t_id = (NSInteger)[data objectAtIndex:0]; 
NSString *t_name = (NSString *)[data objectAtIndex:1]; 
NSString *t_rawname = (NSString *)[data objectAtIndex:2]; 


Tag *t = [[Tag alloc] initWithId:(NSInteger)t_id name:t_name rawname:t_rawname]; 

[self.view addSubview:[t view]]; 

[t release]; 
} 

回答

0

的NSOperation使用一個單獨的線程運行,您必須在主線程中調用[addSubview:]。有一個方法[object performSelectorOnMainThread:withObject:waitUntilDone:] - 你可以使用它來添加子視圖。

[self.view performSelectorOnMainThread:@selector(addSubview:) withObject:aView waitUntilDone:YES]; 
相關問題