I have a UIButton. How can I get the x-coordinate of the finger when the user taps on said button. I'm thinking it will involve touchesBegan but how do I only get the value if the "touchesBegan" occurred on the button? And if there is a way (without just monitoring the x, y, width, and height of the button and checking if my touch was in that region) will I need to disable the button's user interaction so the code can handle it and not the built in xib stuff. Thanks!
1 Answers
A “tap” is generally considered to be a touch-down inside the button followed by a touch-up inside the button. You need to figure out whether you want the touch-down-inside event or the touch-up-inside event (or both).
Let's say you want the touch-down-inside event. Write a method like this in your view controller:
- (IBAction)buttonWasTouched:(UIButton *)button forEvent:(UIEvent *)event {
NSSet *touches = [event touchesForView:button];
NSLog(@"button:%@ touches:%@", button, touches);
}
If you're creating the button in a nib, control-click the button and connect its “Touch Down” outlet to the buttonWasTouched:forEvent:
action on your view controller (which is probably File's Owner).
If you're creating the button in code, send an addTarget:action:forControlEvents:
to the button to connect its UIControlEventTouchDown
event to the view controller's buttonWasTouched:forEvent:
action.
If you want to catch the touch-up-inside event, connect its “Touch Up Inside” outlet in the nib, or use UIControlEventTouchUpInside
in code.

- 375,296
- 67
- 796
- 848
-
in my .h file I have an IBAction linked to touchup inside in the nib file.. I also have an IBOutlet linked to the button in the nib file, in the .m file I copy pasted your code and the app is freezing with SigAbrt. – Albert Renshaw Mar 04 '12 at 19:02
-
You need to [set an exception breakpoint](http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4). Then you need to edit your question and paste in the stack trace of the exception. Also paste in the code that's triggering the exception and any related code. – rob mayoff Mar 04 '12 at 19:09
-
It's a completely blank project file except for what I listed in the previous comment but still the breakpoint is triggered with the calling of the above code. I don't know what a stack trace is. Is there any way to do this with touches began and just make sure the "touch" on touches began is touching the UIButton? That seems easier. – Albert Renshaw Mar 04 '12 at 19:17
-
You didn't list any code in the previous comment. There is a bug in your code. It is impossible for me to help you find the bug if you don't show me your code. A stack trace is what show up in Xcode's Debug navigator when the program is paused (because of a SIGABRT or a breakpoint or whatever). – rob mayoff Mar 04 '12 at 19:22
-
I'm sorry what I meant was it's a completely blank project with no code except for your code and the stuff I listed in the previous comment meaning the "-IBAction" and "IBOutlet UIbutton *"In any event it just started working but the NSLog isn't posting anything to the debug window. Do I have to enable any settings to get NSLog to show things? Thanks! – Albert Renshaw Mar 04 '12 at 19:31
-
You don't have to enable any settings to make `NSLog` work. If it's not printing anything, it's not calling that method, which means something isn't hooked up correctly. – rob mayoff Mar 04 '12 at 19:34
-
Okay, I just gave up on that method and was able to get touches began working by checking that the NSTouch was touching the UIButton with some code I found on google. Thanks though. – Albert Renshaw Mar 04 '12 at 19:57