gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:10:52 24 4
gpt4 key购买 nike

我有一个在单击 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com