One Method For Several Control Events?

Discussion in 'iOS Programming' started by Darkroom, Jan 24, 2010.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i understand it's not typical to connect/call only one method for different control events, but i would like to know if it's even possible.

    example: a button targets a method, but the method executes different code depending on if the button is being pressed or being released.

    Code:
    - (IBAction)buttonAction
    {
    if (UIControlEvent == UIControlEvenTouchDown) NSLog(@"button pushed");
    if (UIControlEvent == UIControlEventTouchUpInside) NSLog(@"button released");
    }
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Certainly possible. Connect different IBActions to different events. Look at all the events that are available for button connections:
     

    Attached Files:

  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    right. however, i'd like to connect the control events to one method, and manage them thru this one method. actually, i'd like to know if it's possible rather than actually do it. is it possible?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    It's certainly possible to connect different events to the same IBAction method. Whether you can manage them from within that method, I'm not sure. If you are just going to logically separate them within the method though, why not just use separate IBActions to start with?
     
  5. icewing macrumors 6502

    Joined:
    Jun 19, 2008
    Location:
    St Louis
    #5
    I've been playing around with Apple's TaggedLocations (think that's the right name) and trying to use the tag property to do this (set the tag value of each control in IB, then check it in the action). I think this is the way to go, but the TaggedLocations code is a little unusual, and is preventing this from working by the way they use delegation....
     

Share This Page