gpt4 book ai didi

iphone - Ntimer 具有变量的方法

转载 作者:行者123 更新时间:2023-12-03 21:21:04 24 4
gpt4 key购买 nike

我有一个具有输入变量的方法,我需要使用NSTimer来安排该方法不幸的是,当我尝试实现这个想法时,我遇到了一些错误我的代码如下:

我的方法:

-(void)movelabel:(UILabel *)label {
}

我使用以下方式进行安排:

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(movelabel:myLbabeName) userInfo:nil repeats:YES];

但是,我收到以下错误:

error: expected ':' before ')' token

在其他情况下(没有输入变量的方法的情况下,我调用计时器如下:

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(myMethodNameWithoutVariable) userInfo:nil repeats:YES];

问候

最佳答案

您提供给 scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: 的选择器不接受任意参数。它应该是不带参数的选择器或具有 (NSTimer *) 类型的单个参数的选择器。

这意味着您无法使用参数 myLbabeName 直接调用 moveLabel:

您可以将 userInfo 字典与中间方法一起使用,如下所示:

(timerRef 是一个 NSTimer 类变量)

timerRef = [NSTimer scheduledTimerWithTimeInterval:0.1 
target:self
selector:@selector(timerMovelabel:)
userInfo:[NSDictionary dictionaryWithObject:myLbabeName
forKey:@"name"]
repeats:YES];

- (void)timerMovelabel:(NSTimer *)timer {
[self movelabel:[[timer userInfo] objectForKey:@"name"]];
}

编辑

如果您想停止计时器,请保留对其的引用并调用[timerRef invalidate]

关于iphone - Ntimer 具有变量的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4554541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com