0

NSOpeationを使用すると、奇妙な動作が発生します。UIButtonを作成してビューに追加する関数(-createTagView)を呼び出しています。何らかの理由でそれらを追加していません。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];
}
4

1 に答える 1

0

NSOperationは別のスレッドを使用して実行します。メインスレッド内で[addSubview:]を呼び出す必要があります。メソッド[objectperformSelectorOnMainThread:withObject:waitUntilDone:]があります-これを使用してサブビューを追加できます。

[self.view performSelectorOnMainThread:@selector(addSubview:) withObject:aView waitUntilDone:YES];
于 2010-07-18T20:19:54.607 に答える