【问题标题】:Can I pass delegate as a parameter objective-c我可以将委托作为参数传递给objective-c吗
【发布时间】:2012-06-13 13:29:48
【问题描述】:

我正在使用 NSOperationQueue,我想将新的 NSOperations 添加到 NSOperationQueue。它是一个存在于我拥有的类的单例实例中的队列。如果我可以通过传递委托将所有内容移到静态类中,事情会变得容易得多。

这是我现在的代码 - 这是在 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 - 或者我可以使用某种块?

【问题讨论】:

    标签: objective-c ios nsoperation


    【解决方案1】:

    只需创建适当的 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-14
      • 2015-09-20
      • 2012-07-12
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多