2012-06-13 44 views
11

我正在使用NSOperationQueue,我想將新的NSOperations添加到NSOperationQueue。這是一個居住在我擁有的類的單例實例中的隊列。如果我可以通過傳遞委託將所有內容移動到靜態類中,它會使事情變得更加容易。我可以通過委託作爲參數objective-c

這裏是我的代碼現在,它住在 - 這是一個cellForRowAtIndexPath

NSString *key = [NSString stringWithFormat:@"%@%@",cell.dataItem.ItemID, cell.dataItem.ManufacturerID]; 

     if (![self.imgOperationInQueue valueForKey:key]) { 

      ImageOperation *imgOp = [[ImageOperation alloc] initWithItemID:cell.dataItem.ItemID withManufacturerID:cell.dataItem.ManufacturerID withReurnType:kThumbnail]; 
      imgOp.identifier = [NSString stringWithFormat:@"%i", cell.tag]; 
      imgOp.delegate = self; 
      [[SharedFunctions sharedInstance] addImageOperationToQueue:imgOp]; 
      [imgOp release]; 

      // store these in the dictionary so we don;t queue up requests more than once 
      [self.imgOperationInQueue setValue:cell.dataItem.ItemID forKey:key]; 
     } 

如果我可以添加的委託作爲參數,我可以把所有這些代碼到共享的單例類,並調用它從我應用程序的任何地方。

我想我可以使用NSNotification - 或者我可以使用某種類型的塊嗎?

回答

20

只需創建傳遞給委託的相應init方法即可。

- (id)initWithItemID:(NSString *)itemID 
    withManufacturerID:(NSString *)manufacturerID 
     withReurnType:(NSInteger)type 
      delegate:(id<YourDelegate>)theDelegate 
{ 
    self = [super init]; 
    if (self) 
    { 
     .... // Other assignments 
     self.delegate = theDelegate; 
    } 

    return self; 
}