PDA

View Full Version : touchesForView question




uaecasher
Dec 13, 2009, 12:21 PM
I'm trying to record the number of taps made on a button and depending on number of taps a user would get deferent results, I'm having problem with the 1st part, here is my code:



- (void)touchesBegan:(NSSet *)touches touchesForView:(button *)view {
NSUInteger numTaps = [[touches anyObject] tapCount];

myTextFeild.text = [NSString stringWithFormat:@"%i", numTaps];

}



I get:

error: expected ')' before 'button'

button is an IBOutlet for UIButton.



PhoneyDeveloper
Dec 13, 2009, 01:46 PM
This is the correct prototype

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

uaecasher
Dec 13, 2009, 02:02 PM
This is the correct prototype

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

but I want it to detect only touches made to a button, how to do that?

Thanks

PhoneyDeveloper
Dec 13, 2009, 02:44 PM
UIControl, of which UIButton is one, works at a bit higher level than touches. It will send its action message when it is tapped.

I would not subclass UIButton for this. I'd just put all the logic in the view controller. Set it up so it receives the action message for touch up inside from the button(s). Save the time and then compare the previous time to the current time when you get the touch up inside. This will allow you to determine a tap count.

uaecasher
Dec 14, 2009, 07:53 AM
UIControl, of which UIButton is one, works at a bit higher level than touches. It will send its action message when it is tapped.

I would not subclass UIButton for this. I'd just put all the logic in the view controller. Set it up so it receives the action message for touch up inside from the button(s). Save the time and then compare the previous time to the current time when you get the touch up inside. This will allow you to determine a tap count.

I'm not sure how to this

robbieduncan
Dec 14, 2009, 08:00 AM
1) What is your type button (as in (button *) view). Making up types will never work.

2) As noted above substituting methods you wished existed for ones that actually do exist will never work.

3) Learn to read the documentation. There is an excellent document on event handling (http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/EventHandling/EventHandling.html#//apple_ref/doc/uid/TP40007072-CH9) that would lead you to the touchesForView: (http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIEvent_Class/Reference/Reference.html#//apple_ref/occ/instm/UIEvent/touchesForView:) method of UIEvent. The ability to use, read and understand the documentation is a core skill that must be developed.

PhoneyDeveloper
Dec 14, 2009, 03:11 PM
I'm not sure how to this

UIButtons and other controls work by sending actions to a target when they are touched. I recommend that you read about this in the UIControl docs and UIButton docs and the event handling docs. Set up an action method in your view controller that is messaged on touch up inside from the button. Once you have that working it's a simple matter of logic to keep count of the touches.