【问题标题】:Can i use two delegates: delegateA in classB, delegateB in classA?我可以使用两个代表:课堂上的delegateS,课堂上的delegateS吗?
【发布时间】:2012-06-24 22:17:06
【问题描述】:

我在 HelloWorldLayer 中有我的代码,我正在使用委托来更改 ScoreLayer 中的分数,我想将消息从 ScoreLayer 发送回到 HelloWorldLayer,以便用新图像更改用户界面。

是否可以在每个类中创建一个委托(在 ScoreLayer 中创建一个 HelloW... 的代表,在 HelloW... 中创建一个 ScoreLayer 的代表)?类似的东西:

hellolayer.delegate = scoreLayer;
scoreLayer.powerUpDelegate = hellolayer;

?

@class MyClass 不起作用:协议未被识别。
"#import "...":其中一个协议无法识别,但我想会有问题,因为 classA 将导入 classB,而后者将导入又是classA等等。

我该怎么办?以下是部分代码:

    //in HelloWorldLayer.h :

#import "ScoreLayer.h"

@protocol PowerUpDelegate
-(void)scalePowerUp;
@end


// HelloWorldLayer
@interface HelloWorldLayer : CCLayer <PowerUpDelegate>
{ … }

@property (nonatomic,retain) id <ScoreDelegate> delegate;




//in ScoreLayer.h :

//@class HelloWorldLayer; -->does not recognize the protocol
#import "HelloWorldLayer.h"

@protocol ScoreDelegate
//...
@end

@interface ScoreLayer : CCLayer <ScoreDelegate>{
    //...
}

@property (nonatomic,retain) id <PowerUpDelegate> powerUpDelegate;//-->cannot find protocol definition...

谢谢

【问题讨论】:

    标签: objective-c delegates protocols


    【解决方案1】:

    我建议使用实现这两种协议的第三个类并使用该类来处理委托方法。

    【讨论】:

      【解决方案2】:

      是的;你可以像预先声明类一样预先声明协议,所以把它放在 ScoreLayer 类的顶部:

      @protocol PowerUpDelegate;
      

      【讨论】:

      • 谢谢!我不知道,我创建了一个 3d 类,它工作正常,谢谢你的回答!
      猜你喜欢
      • 2020-03-21
      • 2016-08-02
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多