如何将参数传递给

如何将参数传递给

本文介绍了如何将参数传递给@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:

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

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()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!