objective-c - Objective-C : Sending arguments to a method called by a UIButton

标签 objective-c ios timer uibutton arguments

我有一个在单击 UIButton 时调用的方法。当我创建按钮时,我希望它存储一个 NSTimer 作为参数。

这是计时器和 UIButton 的创建。我将如何添加要发送到该方法的计时器?我试过 withObject:timer 但它给了我一个警告并在运行时崩溃。

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(0.009) target:self selector:@selector(moveStickFig:) userInfo:stickFig repeats:YES];
[stickFig addTarget:self action:@selector(tapFig:andTime:) forControlEvents:UIControlEventTouchUpInside];

这是我将其发送到的方法:

-(void) tapFig:(id)sender andTime:(NSTimer *)timer

在定义 UIButton 之后,我还尝试了 [stickFig performSelector:@selector(tapFig:andTime) withObject:nil withObject:timer],但这也会导致警告和崩溃。

最佳答案

你不能 - UIControl action selectors调用时不带参数、作为操作源的控件或作为操作源的控件以及在该控件上发生的 UIEvent。在 IB 中,您必须将 UIButton 连接到这样的方法:您不能添加任何其他自定义参数。

如果你想让它访问其他对象,它们需要是实例变量。

评论 Apple's Introduction to Objective C如果您想了解如何定义实例变量。

关于objective-c - Objective-C : Sending arguments to a method called by a UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4844146/

相关文章:

ios - 如何获得 UITextView 的高度应该给出文本的宽度和长度?

objective-c - Hello World Objective-C 和 Xcode 中的错误

objective-c - 在类方法中使用 self

ios - Objective-C/CocoaHTTPServer - 是否可以将响应返回到应用程序?

delphi - 使用 `TStopWatch` 而不使用 `free`

r - 在适用于 R 的 Shiny 应用程序中,如何延迟响应式的触发?

ios - 使用哪个 Google Analytics cocoapods pod

ios - 如何取消 AWSTask<AWSS3TransferUtilityUploadTask *> 请求?

ios - 如何在闭包中访问类的变量?

java - 清理我的延迟计时器