Objective c 在@selector中使用传递的变量实现方法
其中:Objective c 在@selector中使用传递的变量实现方法,objective-c,Objective C,其中: distanceTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(applyShot:newPositionOne.x with:newPositionOne.y) userInfo:nil repeats:NO]; ^^这不管用 必须与此相等 [self applyShot:newPositionOne.x with:newPositionOne.y]; 在运行这个方
distanceTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(applyShot:newPositionOne.x with:newPositionOne.y) userInfo:nil repeats:NO];
^^这不管用
必须与此相等
[self applyShot:newPositionOne.x with:newPositionOne.y];
在运行这个方法之前,我基本上需要一个延迟,它会传递变量,因为它们在方法运行时会不同,所以它必须以某种方式记住它们
然而,我一辈子都不知道如何在@selector中传递变量
例如,我以前用button.tag做过,但从来没有这样做过
任何帮助都将不胜感激,谢谢
我知道我可以设置全局变量,但是可以传递它们吗?好的,那么您可以做一些事情。您是正确的,很难将变量传递到
@selector()
声明中。这里有两个选项:
1) 调用处理变量的不同方法(在类中定义)。例如:
distanceTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(applyShot) userInfo:nil repeats:NO];
- (void)applyShot
{
// manipulate variables and do your logic here
}
2) 将字典传递到的userInfo参数:
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
此词典可以如下所示:
NSDictionary *params = @{@"newXPosition: @(90), @""newYPosition: @(100)"};
然后只需更改您的
applyShot:
方法,以接受NSDictionary
参数,然后解析字典中的相关值并“应用快照”。好的,您可以做几件事。您是正确的,很难将变量传递到@selector()
声明中。这里有两个选项:
1) 调用处理变量的不同方法(在类中定义)。例如:
distanceTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(applyShot) userInfo:nil repeats:NO];
- (void)applyShot
{
// manipulate variables and do your logic here
}
2) 将字典传递到的userInfo参数:
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
此词典可以如下所示:
NSDictionary *params = @{@"newXPosition: @(90), @""newYPosition: @(100)"};
然后只需更改您的
applyShot:
方法,以接受NSDictionary
参数,然后解析字典中的相关值并“应用快照”。好的,您可以做几件事。您是正确的,很难将变量传递到@selector()
声明中。这里有两个选项:
1) 调用处理变量的不同方法(在类中定义)。例如:
distanceTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(applyShot) userInfo:nil repeats:NO];
- (void)applyShot
{
// manipulate variables and do your logic here
}
2) 将字典传递到的userInfo参数:
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
此词典可以如下所示:
NSDictionary *params = @{@"newXPosition: @(90), @""newYPosition: @(100)"};
然后只需更改您的
applyShot:
方法,以接受NSDictionary
参数,然后解析字典中的相关值并“应用快照”。好的,您可以做几件事。您是正确的,很难将变量传递到@selector()
声明中。这里有两个选项:
1) 调用处理变量的不同方法(在类中定义)。例如:
distanceTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(applyShot) userInfo:nil repeats:NO];
- (void)applyShot
{
// manipulate variables and do your logic here
}
2) 将字典传递到的userInfo参数:
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
此词典可以如下所示:
NSDictionary *params = @{@"newXPosition: @(90), @""newYPosition: @(100)"};
然后,只需将您的
applyShot:
方法更改为接受NSDictionary
参数,然后在字典中进行解析以找到相关值并“应用快照”。选择器不是打包的方法。选择器只是消息的名称。您可以将其视为一个字符串(它过去被实现为一个字符串)
解决这个问题的传统方法是使用NSInvocation
,它打包了一个完整的方法调用,而不仅仅是消息的名称。这很好地涵盖了。另一种处理方法是将选项打包到userInfo
中,并更改方法以从中读取其参数
但通常今天更好的解决方案是使用dispatch\u after
:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC),
dispatch_get_main_queue(),
^(void){
[self applyShot:newPositionOne.x with:newPositionOne.y];
});
选择器不是打包的方法。选择器只是消息的名称。您可以将其视为一个字符串(它过去被实现为一个字符串) 解决这个问题的传统方法是使用
NSInvocation
,它打包了一个完整的方法调用,而不仅仅是消息的名称。这很好地涵盖了。另一种处理方法是将选项打包到userInfo
中,并更改方法以从中读取其参数
但通常今天更好的解决方案是使用dispatch\u after
:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC),
dispatch_get_main_queue(),
^(void){
[self applyShot:newPositionOne.x with:newPositionOne.y];
});
选择器不是打包的方法。选择器只是消息的名称。您可以将其视为一个字符串(它过去被实现为一个字符串) 解决这个问题的传统方法是使用
NSInvocation
,它打包了一个完整的方法调用,而不仅仅是消息的名称。这很好地涵盖了。另一种处理方法是将选项打包到userInfo
中,并更改方法以从中读取其参数
但通常今天更好的解决方案是使用dispatch\u after
:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC),
dispatch_get_main_queue(),
^(void){
[self applyShot:newPositionOne.x with:newPositionOne.y];
});
选择器不是打包的方法。选择器只是消息的名称。您可以将其视为一个字符串(它过去被实现为一个字符串) 解决这个问题的传统方法是使用
NSInvocation
,它打包了一个完整的方法调用,而不仅仅是消息的名称。这很好地涵盖了。另一种处理方法是将选项打包到userInfo
中,并更改方法以从中读取其参数
但通常今天更好的解决方案是使用dispatch\u after
:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC),
dispatch_get_main_queue(),
^(void){
[self applyShot:newPositionOne.x with:newPositionOne.y];
});
我真正遇到的问题是,这是为了模拟一颗子弹所经过的距离,所以他们按下了火,一秒钟后你就会看到撞击。但是,如果他们在第一枪命中之前发射另一枪,它将渲染最新的一枪
posX&posY
,从而删除第一枪的预期位置。我认为字典是我最好的选择,然而,它能解释这个问题吗?这听起来像是一个新问题。我