2012-06-13 19 views
11

Sto lavorando con un NSOperationQueue e voglio aggiungere nuove NSOperations a NSOperationQueue. È una coda che vive in un'istanza singleton di una classe che ho. Sarebbe molto più semplice se potessi spostare tutto nella classe statica passando il delegato.Posso passare delegato come parametro obiettivo-c

Ecco il mio codice ora come vive in - questo è in un 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]; 
     } 

Se potessi aggiungere il delegato come parametro ho potuto mettere tutto questo codice nella classe Singleton condiviso e chiamarlo da qualsiasi punto della mia app.

Suppongo che potrei usare un NSNotification - o posso usare un blocco di qualche tipo?

risposta

20

Basta creare il metodo init appropriato che passa nel delegato.

- (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; 
} 
Problemi correlati