I have a UIButton which sends the draginside event to:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
It does this through the following code in viewDidLoad:
[colourButton1 addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];
Within the method it sends it to, there is the following line:
UITouch *myTouch = [touches anyObject];
And for some reason, when dragging inside the UIButton this crashes the app. Any ideas why?
EDIT: The solution..
-(IBAction)buttonDragged:(id)button withEvent:(UIEvent*)event {
NSSet *touches = [event touchesForView:colourButton1];
[self touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event];
}
When you’re adding target to control you can pass 3 types of selectors for action –
Names don’t matter, it’s number of parameter which is important. If control sends it’s target a message with 2 params then first parameter will be control itself (instance of
UIButtonin your case), second one – instance ofUIEvent. But you expects instance ofNSSetas first parameter and send it a messageanyObjectwhichUIButtondoesn’t understand. This is the cause of crash.Why are you trying to send event from UI control to touch handling method
touchesMoved:withEvent:in the first place? It will probably do something different from what you meant.UPDATE:
Notice that since
touchesMoved:withEvent:is aUIResponder‘s method and controller’s view isUIResponderthis method will be also called on touch events for this view.