I am trying to find a way to use a button click, and not from the response chain as such, but from other action methods associated with this button. I looked through all this solution and could not find it.
For example, let's say I set a button with a selector for an event:
[button addTarget:self action:@selector(handler1:) forControlEvents:UIControlEventTouchUpInside];
Then, in code based on the specific circumstances of the application, I want to add another event handler for the same control event to the same button:
[button addTarget:self action:@selector(handler2:) forControlEvents:UIControlEventTouchUpInside];
This works great , both events are really called. But my question is that, without removing handler1 from the button, how can I make sure that when handler2 is called, the event is "consumed" and handler1 is not called?
The reason I have this circumstance is because I want my application to go into the training mode, where I dynamically bind new events to buttons in the textbook mode. The tutorial will instruct the user to press a specific button, but I want the tap events on other buttons on the screen to be ignored, basically forcing the user to press the requested button to continue working with the tutorial. Thus, each button receives a new TouchUpInside handler when the user enters a tutorial. I want this new handler to be called first and block the execution of the original handler.
I managed to get it called first by getting all the source events in an NSSet , and then calling [button removeTarget...] for all existing events. Then I add the dynamic event and then re-add all the source events from the set. This works in the debugger to show that my dynamic event is really called first.
- For example:
- handler1 will do something when pressed (default handler for the button)
- handler2 is added dynamically and will interact with the training controller, "consuming" the tap event (preventing the execution of handler1).
If not in tutorial mode, I want handler1 to still do what it should do, but if I have handler2, I want this method to execute and then prevent handler1 from being called. I canβt lose the handler1 from the button, because when the tutorial ends, I want the application to work as intended. In addition, I may have certain cases where I still want to call handler1.
So, is it possible to destroy an event and hold on to other related events?
I tried to make [button resignFirstResponder] in [button resignFirstResponder] , but this does not work. It still calls the source button event handler.