multiple UIButtons with same IBAction -how to know which UIButton is clicked

Discussion in 'iOS Programming' started by newlearner, Oct 20, 2010.

  1. newlearner macrumors member

    newlearner

    Joined:
    Jul 30, 2009
    Location:
    india
    #1
    I have 3 buttons in a single view. All three can be given the same IBAction. But how to know which button was pressed?

    I tried implementing the following code:

    Code:
     
    -(IBAction)callAction:(id)sender {
    
    UIButton *b=(UIButton *)sender;
    NSLog(@"button pressed is : %d", [sender tag]);
    }
    
    
    But for all buttons, the answer is always 0.

    Is there something I am missing here?

    Or is there any other alternative?

    Thanks
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Setting the tag values for each of the buttons?
     
  3. newlearner thread starter macrumors member

    newlearner

    Joined:
    Jul 30, 2009
    Location:
    india
    #3
    How to set the tag values? I looked under in the documentation for UIButton, but it doesnt have any mention for tags. Could you please guide me?
     
  4. newlearner thread starter macrumors member

    newlearner

    Joined:
    Jul 30, 2009
    Location:
    india
    #4
    Got it.

    Code:
    
    button.tag=0;
    
    
    Strange it is not mentioned in the docs.

    thanks dejo.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Read the docs for UIButton's superclass. Then that class's superclass.
     
  6. newlearner thread starter macrumors member

    newlearner

    Joined:
    Jul 30, 2009
    Location:
    india
    #6
    But UIControl and UIView also dont mention about 'tag'.

    Am sorry if it sounds silly, am not able to get the correct link for reading about it.

    found it.

    NSControl mentions it. Not UIControl.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    There is no NSControl in the iPhone SDK. And UIView does have such a property.
     
  8. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #8
    You can set tag numbers for the controls and check against the tag numbers.
     

Share This Page