UIBarButtonItem actions

Discussion in 'iOS Programming' started by North Bronson, Jan 11, 2010.

  1. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #1
    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?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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.
     
  3. North Bronson thread starter macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #3
    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.
     

Share This Page