如何防止双击 UIButton 按钮取消操作?
我有一个 UIButton,单击它后开始搜索数据。但是在搜索时,如果用户再次单击该按钮,则会取消搜索。
相关代码:
- (void)searchAction:(UIButton *)sender {
[sender removeTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
[sender addTarget:self action:@selector(cancelSearch:) forControlEvents:UIControlEventTouchUpInside];
[animatedImages startAnimating];
//action that should be done
}
- (void)cancelSearch:(UIButton *)sender {
[sender removeTarget:self action:@selector(cancelSearch:) forControlEvents:UIControlEventTouchUpInside];
[sender addTarget:self action:@selector(searchNearbyAction:) forControlEvents:UIControlEventTouchDown];
[animatedImages stopAnimating];
}
所以基本思想是,当我单击它时,它的功能更改为取消,反之亦然。
问题是,当我第一次单击它时,它调用 searchAction
然后调用 <无缘无故地代码>cancelSearch。
有谁知道为什么会发生这种情况?
I have a UIButton
that when clicked starts searching for data. But while searching, if the user clicks on the button again it cancels the search.
Relevant Code:
- (void)searchAction:(UIButton *)sender {
[sender removeTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
[sender addTarget:self action:@selector(cancelSearch:) forControlEvents:UIControlEventTouchUpInside];
[animatedImages startAnimating];
//action that should be done
}
- (void)cancelSearch:(UIButton *)sender {
[sender removeTarget:self action:@selector(cancelSearch:) forControlEvents:UIControlEventTouchUpInside];
[sender addTarget:self action:@selector(searchNearbyAction:) forControlEvents:UIControlEventTouchDown];
[animatedImages stopAnimating];
}
So the basic idea is that when i click on it its function change to cancel and vice-verca
The problem is that when I click on it the first time, it calls searchAction
and then calls cancelSearch
for no reason.
Does anyone know why this might be happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方法是添加 BOOL 作为实例变量
,然后执行以下操作
A workaround is to add a BOOL as an instance variable
and then do the following