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?
A workaround is to add a BOOL as an instance variable
and then do the following