在iPhone中调用某个类的SEL(选择器)?

发布于 2025-01-01 15:42:28 字数 1264 浏览 0 评论 0原文

我有一个类,我在其中调用另一个类的方法(带有 SEL 参数),如下所示,

// class 1

+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];

// add layer as a child to scene 
[scene addChild: layer];

// returns a CClayer Object
id myClassLayer = [myClass objWithBackgroundSprite:objBackground withRect:CGRectMake(160,100, 300,120) atPoistion:pos_Top];

SEL callOnClick;
[myClassLayer AddButtonWithImage:ButtonBackground selector:callOnClick withDisabledImage:disabledBackground];

[scene addChild: myClassLayer];

// return the scene
return scene;
}

// class 2

-(void)AddButtonToHUDWithImage:(NSString *)imageName selector:(SEL)selector withDisabledImage:(NSString *)disbdImageName

{

CCMenuItem *menuItem1 = [CCMenuItemImage itemFromNormalImage:imageName selectedImage:imageName disabledImage:disbdImageName target:self selector:@selector(selector)];

CCMenu *menu = [CCMenu menuWithItems:menuItem1,nil];  
[menu setPosition:ccp(160,100)];
[self addChild:menu];
}

-(void)selector
{
    // not creating any object .. some other way if possible
}

但我想仅在 HelloWorld 中声明选择器方法(callOnClick)(myClass 是通用类)。

如何仅在 HelloWorld 中调用选择器?

I have a class in which i am calling a method(with a SEL parameter) of another class like as follows

// class 1

+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];

// add layer as a child to scene 
[scene addChild: layer];

// returns a CClayer Object
id myClassLayer = [myClass objWithBackgroundSprite:objBackground withRect:CGRectMake(160,100, 300,120) atPoistion:pos_Top];

SEL callOnClick;
[myClassLayer AddButtonWithImage:ButtonBackground selector:callOnClick withDisabledImage:disabledBackground];

[scene addChild: myClassLayer];

// return the scene
return scene;
}

// class 2

-(void)AddButtonToHUDWithImage:(NSString *)imageName selector:(SEL)selector withDisabledImage:(NSString *)disbdImageName

{

CCMenuItem *menuItem1 = [CCMenuItemImage itemFromNormalImage:imageName selectedImage:imageName disabledImage:disbdImageName target:self selector:@selector(selector)];

CCMenu *menu = [CCMenu menuWithItems:menuItem1,nil];  
[menu setPosition:ccp(160,100)];
[self addChild:menu];
}

-(void)selector
{
    // not creating any object .. some other way if possible
}

but i would like to declare the selector method(callOnClick) in HelloWorld only(myClass being a Generic Class).

How can i call the selector in HelloWorld only??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你げ笑在眉眼 2025-01-08 15:42:28

如果不需要实例,则可以使用类方法,目标/对象将为 [SOMEType class][self class]

+ (void)selector // <<    + not -
{
    // not creating any object .. some other way if possible
}

If you need no instance, then you can use a class method and the target/object will be [SOMEType class] or [self class]:

+ (void)selector // <<    + not -
{
    // not creating any object .. some other way if possible
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文