ios - 多次点击 UIButton 多次触发 Target 函数

标签 ios iphone uibutton actionmethod uicontrolevents

我有一个 UIButton。我绑定(bind)了一个目标如下。

[button addTarget:self action:@selector(myFunction) 
                forControlEvents:UIControlEventTouchUpInside];

当我快速多次单击我的按钮时,它会多次调用目标函数。 在点击按钮上,我展示了一个新的 View Controller 。 当我快速单击 3 次时,我的新 View Controller 将显示 3 次。

这是愚蠢的事情。一旦 View 已转移到新的 View Controller ,再次触发该功能有什么意义。为什么 hell 苹果会做出如此愚蠢的事情?

有什么帮助吗?

最佳答案

首先,它不是 apple bug。应该手动处理。所以按照这些步骤

首先创建按钮的全局实例,然后执行此操作

.h文件

@property (weak, nonatomic) IBOutlet UIButton *btn;

.m文件

- (IBAction)myFunction:(id)sender
{
    self.btn.userInteractionEnabled = NO;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.btn.userInteractionEnabled = YES;
}

关于ios - 多次点击 UIButton 多次触发 Target 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30972471/

相关文章:

swift - 如何根据条件停止按钮动画

ios - 在 SWTableViewCell 中,UtilityButtons 不响应 setTag 属性

ios - 无法在Xcode的CS193p项目的SwiftUI'文本'中推断通用类型'S'

ios - AFNetworking 2 + SwiftyJSON

iphone - 当按钮快速按下iPhone obj-c时,阻止此应用程序崩溃的最佳方法是什么?

iphone - 如何给iPhone钥匙串(keychain)添加安全身份(证书+私钥)?

iphone - 如何显示来自 URL 的图像并在点击 UIButton 时显示它?

ios - 在iPhone应用程序中运行时进行内存泄漏检测

iOS:键入时搜索建议

c# - 统一 5 : How to pass multiple parameters on button click function from inspector?