Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

North Bronson

macrumors 6502
Original poster
Oct 31, 2007
395
1
San José
I have a UIToolbar on my view with a UIBarButtonItem. I want to know if there is a way to have the UIBarButtonItem send an action when the user touches down. I want to be able to let the user perform a certain action when the user touches up from the UIBarButtonItem (the normal behavior), but if the user holds down on the UIBarButtonItem for two seconds, a different action occurs.

Basically, I am trying to start a timer when the user touches down, but I cannot figure out where I could make this happen. Any ideas? Will I only have this functionality with a UIControl?
 
Have you setup your UIBarButtonItem via Interface Builder or programmatically? If the former, connect the the "Touch Down" event of the button to an IBAction method. If the latter, I'm not exactly sure how you code that kinda of connection. Let me know if that's your approach, though. Then in this method, you could set an ivar to the current date-time ([NSDate date]). In the touch-up method, you can see if this time was set and if more than two seconds have elapsed. If so, perform your different action.
 
Have you setup your UIBarButtonItem via Interface Builder or programmatically? If the former, connect the the "Touch Down" event of the button to an IBAction method. If the latter, I'm not exactly sure how you code that kinda of connection. Let me know if that's your approach, though. Then in this method, you could set an ivar to the current date-time ([NSDate date]). In the touch-up method, you can see if this time was set and if more than two seconds have elapsed. If so, perform your different action.

I do everything in code, but I did take a look at IB. I thought about your approach, but since UIBarButtonItem does not inherit from UIControl (UIBarButtonItem : UIBarItem : NSObject), there doesn't seem to be a touch down event.

I also thought about subclassing UIBarButtonItem and overriding "setHighlighted:" to start the timer there, but I couldn't find a method close to that in either UIBarButtonItem or UIToolBar. :confused:

Does anyone know the chain of public events when a UIBarButtonItem in a UIToolBar is touched? I guess I could try looking through the debugger to find out.

I guess another way would be to add a transparent UIControl over the UIBarButtonItem.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.