目标C:发送参数的方法被称为的UIButton [英] Objective C: Sending arguments to a method called by a UIButton

查看:115
本文介绍了目标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:定时器,但它给了我在运行时发出警告和崩溃。

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];

这是我送下来的方法:

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

我也试过 [stickFig performSelector:@selector(tapFig:andTime)withObject:无withObject:定时器]我定义的UIButton 之后,但也导致警告和崩溃。

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 <一个href=\"https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/Target-Action/Target-Action.html#//apple_ref/doc/uid/TP40010810-CH12-SW44\"相对=nofollow>行动选择调用不带任何参数,那就是动作的来源,或者说是动作的来源和发生在该控制的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.

回顾<一个href=\"http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html\"相对=nofollow>苹果公司的简介目标C 如果你想了解如何定义实例变量。

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

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

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