2012-06-13 12 views
11

Pracuję z NSOperationQueue i chcę dodać nowe NSOperations do NSOperationQueue. Jest to kolejka, która żyje w pojedynczym egzemplarzu klasy, którą mam. Sprawiłoby to o wiele łatwiejsze, gdybym mógł przenieść wszystko do klasy statycznej, przekazując delegata.Czy mogę przekazać delegate jako parametr celu-c

Oto mój kod teraz, gdyż mieszka w - to w 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]; 
     } 

Jeśli mógłbym dodać delegata jako parametr może umieścić cały ten kod do wspólnej klasy singleton i nazwać z dowolnego miejsca w mojej aplikacji.

Przypuszczam, że mogę użyć NSNotification - czy mogę użyć jakiegoś bloku?

Odpowiedz

20

Po prostu utwórz odpowiednią metodę init, która przechodzi w delegata.

- (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; 
} 
Powiązane problemy