将对象传递给 NSTimer 中的方法
这真是一个愚蠢的问题。如何使用 NSTimer 将对象传递给方法? 我的意思是这样的 -
我在 BigView.m
中有一个方法,它有一个名为 doSomethingWithClass:
的方法。
- (void)doSomethingWithClass:(CustomClass *)class {
NSLog(@"Something was done");
}
在另一个名为 CustomClass
的类中,我有一个 NSTimer -
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:bigView selector:@selector(doSomethingWithClass:) userInfo:nil repeats:NO];
其中 bigView 是 BigView 的实例。现在我想将 CustomClass 的整个实例作为 doSomethingWithClass:
方法中的参数传递。我该怎么做?
This is a really dumb question. How would you pass an object to a method using an NSTimer?
I mean something like this -
I have a method in BigView.m
that has a method called doSomethingWithClass:
.
- (void)doSomethingWithClass:(CustomClass *)class {
NSLog(@"Something was done");
}
In another class called CustomClass
, I have an NSTimer -
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:bigView selector:@selector(doSomethingWithClass:) userInfo:nil repeats:NO];
Where bigView is an instance of BigView. Now I want to pass an entire instance of CustomClass as the parameter in the method doSomethingWithClass:
. How do I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不需要引用计时器,请使用更简单的
performSelector:withObject:afterDelay:
方法。(要取消它,使用
+cancelPreviousPerformRequestsWithTarget:...
。)If you don't need to refer to the timer, use the simpler
performSelector:withObject:afterDelay:
method.(To cancel it, use
+cancelPreviousPerformRequestsWithTarget:…
.)