如何将参数传递给@selector()? [英] How do I pass an argument to @selector()?

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

问题描述

我有以下NSTimer呼叫

I have the following NSTimer call

[NSTimer scheduledTimerWithTimeInterval:2.0
                               target:self
                             selector:@selector(panelVisibility:)
                             userInfo:nil
                              repeats:NO];

-(void)panelVisibility:(BOOL)visible{
...
}

我需要将BOOL值传递给panelVisibility方法.如何指定参数值?

where I need to pass a BOOL value to the panelVisibility method. How do I specify the parameter value?

推荐答案

在这种情况下,您不需要这样做.查看参考文档:

In this instance, you don't. Check the reference docs:

aSelector
计时器触发时发送到目标的消息.这 选择器必须具有以下内容 签名:

aSelector
The message to send to target when the timer fires. The selector must have the following signature:

  • (void)timerFireMethod:(NSTimer *)theTimer

计时器将自身传递为 此方法的参数.

The timer passes itself as the argument to this method.

因此,您的panelVisibility:方法可以接受的唯一参数是NSTimer*,计时器将自动为您传递该参数.

So the only parameter your panelVisibility: method can accept is an NSTimer*, and the timer will pass this in automatically for you.

但是,您可以使用userInfo字段传递您想要的任何其他信息.因此,您可以执行以下操作:

What you can do, however, is use the userInfo field to pass whatever other information you want. So you could, for instance, do:

[NSTimer scheduledTimerWithTimeInterval:2.0
                               target:self
                               selector:@selector(panelVisibility:)
                               userInfo:[NSNumber numberWithBool: myBool]
                               repeats:NO];

...然后拥有:

-(void)panelVisibility:(NSTimer*)theTimer{
    BOOL visible = [theTimer.userInfo boolValue];
    //...
}

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

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