开发者

Delegate works in controller A but not in controller B

This is the delegate

@protocol DropControllerDelegate;

@interface DropController : NSObject
   id<DropControllerDelegate> delegate;
}

@property (nonatomic, assign) id<DropControllerDelegate> delegate;
+ (DropController*) sharedController;

@protocol DropControllerDelegate <NSObject>
- (void)ope开发者_如何学GonUserButtons;
- (void)startUpload;
- (void)uploadDone;
- (void)uploadFailed;
- (void)startDownload;
- (void)downloadDone;
- (void)subFolderLoaded;
@end

This is the singleton code:

static DropController *sharedCont = nil;

#pragma mark Singleton stuff

+ (DropController *) sharedController {
    @synchronized(self)     {
        if (!sharedCont)
            sharedCont = [[DropController alloc] init];
        return sharedCont;
    }
    return sharedCont;
}

The code to set the delegate is (in both myControllerA and myControllerB):

DropController* dropHelper = [DropController sharedController];
dropHelper.delegate = self;

I'm able to receive the calls in one controller but not in the other controller (the code is a copycat), this is driving me crazy!


As i said in my comment, since you are using a shared instance of that object, it will overwrite the previously set delegate to the new object. So i would suggest to hold a array in your sharedController. Add objects to that array which will want to become the delegates to the sharedController.

Then iterate through that array and call the method over each object in it. Its pretty simple.


Edit:

When you assign the delegate in your viewcontroller A with the statement dropHelper.delegate = self; object A becomes the delegate. Calling the method [delegate openUserButtons]; will trigger the method in viewcontroller A. But when you do dropHelper.delegate = self; in viewcontroller B, [delegate openUserButtons]; will call the method in object B. It will not call both the methods from a and b. Since there is only one delegate variable and you are using a shared singleton object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜