目标 C:向 UIButton 调用的方法发送参数 [英] Objective C: Sending arguments to a method called by a UIButton

查看:29
本文介绍了目标 C:向 UIButton 调用的方法发送参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a method that is being called when a UIButton is clicked. When I create the button I want it to store an NSTimer as an argument.

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

This is the timer and the creation of the UIButton. How would I add in the timer to be sent down to the method? I've tried withObject:timer but it gives me a warning and crashes at runtime.

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

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

This is the method I'm sending it down to:

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

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

I've also tried [stickFig performSelector:@selector(tapFig:andTime) withObject:nil withObject:timer] after I defined the UIButton, but that also results in a warning and crashes.

推荐答案

你不能 - UIControl 动作选择器 不带参数调用,该控件是动作,或作为动作来源的控件和发生在该控件上的 UIEvent.在 IB 中,您必须将 UIButton 连接到这样的方法:您不能添加任何其他自定义参数.

You can't - UIControl action selectors are invoked with no parameters, the control that is the source of the action, or the control that is the source of the action and the UIEvent which occurred on that control. In IB you have to connect the UIButton to such a method: you can't add any other custom parameters.

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

If you want it to have access to other objects, they need to be instance variables.

回顾 Apple 对 Objective C 的介绍 如果你想了解如何定义实例变量.

Review Apple's Introduction to Objective C if you want to understand how to define instance variables.

这篇关于目标 C:向 UIButton 调用的方法发送参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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