将方法作为参数传递
我可以传递一个方法作为参数吗? 在下面的示例中,我没有成功传递 targetOpenView 方法:
-(void) targetTimeView:(id)sender {
[self TimeViewWithtimeInterval:.6 selector:targetOpenView]; //targetOpenView does NOT work
}
-(void) timeViewWithtimeInterval:(float)interval selector:openViewMethod{
[NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(openViewMethod) userInfo:nil repeats:NO];
}
有什么建议可以让我完成这项工作吗?谢谢!
can I pass a method as an argument?
I don't succeed to pass the method targetOpenView in the example below:
-(void) targetTimeView:(id)sender {
[self TimeViewWithtimeInterval:.6 selector:targetOpenView]; //targetOpenView does NOT work
}
-(void) timeViewWithtimeInterval:(float)interval selector:openViewMethod{
[NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(openViewMethod) userInfo:nil repeats:NO];
}
Any suggestions how I could make this work? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要 @selector 编译器指令从方法名称中提取选择,就像创建计时器时所做的那样:
并将参数定义为类型
SEL
:然后,当将参数传递给 NSTimer 方法,您可以省略
@selector
因为该类型已经是一个选择器:You need the
@selector
compiler directive to extract the select from a method name, like you did when creating the timer:And define your argument to the type
SEL
:Then, when passing the argument to the NSTimer method you can leave off the
@selector
since the type is already a selector: