无法将参数传递给@selector方法? [英] Can't pass arguments into @selector methods?

查看:134
本文介绍了无法将参数传递给@selector方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用标题为X的UIButton作为从视图中删除Sprite的方法。

I'm currently trying to use a UIButton titled "X" as a way to remove a Sprite from the view.

基本上,我的代码可以正常运行触摸Sprite,将消息发送给委托者(View Controller),该委托传递已选择的(Sprite *)精灵。在这个方法中,我在该sprite之上绘制一个UIButton。到目前为止,非常好。

Basically, my code works so that when a Sprite is touched, a message is sent to the delegate (View Controller) that passes the (Sprite *)sprite which has been selected. In this method, I draw a UIButton on top of that sprite. So far, so good.

然而,问题是我现在希望我的UIButton在按下按钮时运行@selector来删除该精灵。

However, the issue is I now want my UIButton to run a @selector to remove that sprite whenever the button is touched down.

最初我试过这个:

-(void)spriteSelected:(Sprite *)sprite{ //delegate method implementation
      [sprite.button addTarget:self action:@selector(removeSprite:sprite) forControlEvents: UIControlEventTouchDown]
}    

-(void)removeSprite:(Sprite *)sprite{
        [sprite removeFromSuperView];}

然而,似乎我不能像这样把参数放到选择器中。关于我如何调整这个的任何想法?

However, it seems I can't put arguments into the selector like that. Any ideas on how I can adjust this?

谢谢

推荐答案

您应该在 Sprite 类中执行此操作,因此在原始类中:

You should do this in the Sprite class, so in the original class:

-(void)spriteSelected:(Sprite *)sprite{
      [sprite youAreSelected];
} 

Sprite class:

-(void)youAreSelected {
    [self.button addTarget:self action:@selector(removeMe:) forControlEvents: UIControlEventTouchDown];
}

-(void)removeMe:(id)sender {
    [self removeFromSuperView];
}

这篇关于无法将参数传递给@selector方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆