如何通过选择器方法传递 int 值?

发布于 2024-12-11 19:46:49 字数 440 浏览 0 评论 0原文

我想从选择器方法传递一个 int 值,但选择器方法只接受一个对象类型参数。

int y =0;
[self performselector:@selector(tabledata:) withObject:y afterDelay:0.1];

方法执行在这里

-(int)tabledata:(int)cellnumber {
   NSLog(@"cellnumber: %@",cellnumber);
   idLabel.text = [NSString stringWithFormat:@"Order Id: %@",[[records objectAtIndex:cellnumber] objectAtIndex:0]];
}

,但我没有在我的方法中获得精确的整数值,我只获得 id 值。

I want to pass an int value from my selector method, but the selector method takes only an object type parameter.

int y =0;
[self performselector:@selector(tabledata:) withObject:y afterDelay:0.1];

Method execution is here

-(int)tabledata:(int)cellnumber {
   NSLog(@"cellnumber: %@",cellnumber);
   idLabel.text = [NSString stringWithFormat:@"Order Id: %@",[[records objectAtIndex:cellnumber] objectAtIndex:0]];
}

but I am not getting exact integer value in my method, I am only getting the id value.

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

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

发布评论

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

评论(3

懒猫 2024-12-18 19:46:49

如果您“拥有”目标选择器,最简单的解决方案是将 int 参数包装在 NSNumber 中:

-(int)tabledata:(NSNumber *)_cellnumber {
    int cellnumber = [_cellnumber intValue];
    ....
}

要调用此方法,您将使用:

[self performselector:@selector(tabledata:) withObject:[NSNumber numberWithInt:y] afterDelay:0.1];

The easiest solution, if you 'own' the target selector, is to wrap the int argument in an NSNumber:

-(int)tabledata:(NSNumber *)_cellnumber {
    int cellnumber = [_cellnumber intValue];
    ....
}

To call this method you would use:

[self performselector:@selector(tabledata:) withObject:[NSNumber numberWithInt:y] afterDelay:0.1];
救星 2024-12-18 19:46:49

这也适用于 int 参数,如果您无法更改要执行的选择器的签名,则该参数特别有用。

SEL sel = @selector(tabledata:);

NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:sel];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.selector = sel;
// note that the first argument has index 2!
[invocation setArgument:&y atIndex:2];

// with delay
[invocation performSelector:@selector(invokeWithTarget:) withObject:self afterDelay:0.1];

This works also for an int parameter, which is especially useful, if you are unable to change the signature of the selector you want to perform.

SEL sel = @selector(tabledata:);

NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:sel];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.selector = sel;
// note that the first argument has index 2!
[invocation setArgument:&y atIndex:2];

// with delay
[invocation performSelector:@selector(invokeWithTarget:) withObject:self afterDelay:0.1];
梦在夏天 2024-12-18 19:46:49

使用 NSTimer 代替 PerformSelector:withObject:afterDelay:,这样:

int y = 0;
[NSTimer scheduledTimerWithTimeInterval:0.1 repeats:NO block:^(NSTimer *timer) {
    [self tabledata:y];
}];

您可以在计时器块中传递您想要的任何内容。

Instead of your performSelector:withObject:afterDelay:, use an NSTimer, thusly:

int y = 0;
[NSTimer scheduledTimerWithTimeInterval:0.1 repeats:NO block:^(NSTimer *timer) {
    [self tabledata:y];
}];

You can pass whatever you want in the timer block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文