Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Using tags in Interface Builder

Mike JMike J Posts: 110Registered Users
edited August 2009 in iPhone SDK Development
I know that in interface builder that you can assign tags to different labels and buttons. I was wondering how I would make it so that when I pressed one of two buttons with two different tags that it would be able to distinguish the two. I would like to be able to do something like that without multiple functions.

THX
Post edited by Mike J on

Replies

  • kelvinkaokelvinkao Posts: 352Registered Users
    edited August 2009
    You just need to get the UIButton object, and check its tag property to see if it's the one you want. As in
    - (IBAction)buttonClicked:(id)sender
    {
       UIButton* myButton = (UIButton*)sender;
       if (sender.tag == 12)
          ...;  //do something
    }
    
    My dev blog:<br />
    <a href="http://www.kelvinkaodev.com" target="_blank">http://www.kelvinkaodev.com</a>;
  • MichaelEMichaelE Posts: 446Registered Users
    edited August 2009
    One thing I don't like about Objective-C is that you can't strongly type IBActions such as a buttonClick to accept a UIButton instead of an id. This may be minor to some, but it irks me every time I see it. In any case, to get around the issue and make it clear that I'm interested in buttons and nothing else I typically add class checking around the click, such as this:
    - (IBAction)buttonClicked:(id)sender {
    
       if ([sender isKindOfClass:[UIButton class]])
       {
          UIButton* myButton = (UIButton*)sender;
          if (sender.tag == 12)
             ...;  //do something
       }
       else {
          NSLog("Invalid object type sent to buttonClicked action.");
       }
    }
    

    Some would argue that casting to a button without type checking is fine since as a developer you wouldn't hook up, for example, a UISegmentedControl's valueChanged event to the buttonClick IBAction, but you never know how long your code will be in use or who else may work on it. So I always try to keep things strongly typed wherever possible.

    (steps off soapbox)
    kelvinkao wrote: »
    You just need to get the UIButton object, and check its tag property to see if it's the one you want. As in
    - (IBAction)buttonClicked:(id)sender
    {
       UIButton* myButton = (UIButton*)sender;
       if (sender.tag == 12)
          ...;  //do something
    }
    
    Michael Emmons<br />
    Founder, App Apps, LLC<br />
    <a href="http://app-apps.com" target="_blank">http://app-apps.com</a><br />
    <br />
    <a href="http://app-apps.com/apps/videobot-camera-for-ios/" target="_blank">vi
  • kelvinkaokelvinkao Posts: 352Registered Users
    edited August 2009
    Ooo, soapbox! Maybe I'll jump on it too!

    Personally, I prefer writing code like
    - (IBAction)buttonClicked: (id)sender
    {
        UIButton* theButton = (UIButton*)sender;
        if(theButton == mButton1)
           ...
        else if (theButton == mButton2)
           ...
        else if (theButton == mButton3)
           ...
    }
    

    Tags are useful in some certain situations, but if I want to find out which button is clicked, I prefer to just go through them one by one. Even if it's not of type UIButton*, no harm done. Of course, this makes more sense in situations where the buttons are all doing very different things so you want to have separate code for them anyway.
    My dev blog:<br />
    <a href="http://www.kelvinkaodev.com" target="_blank">http://www.kelvinkaodev.com</a>;
  • RLScottRLScott Posts: 1,638Tutorial Authors, Registered Users @ @ @ @
    edited August 2009
    If you have code like this:
    -(IBAction) buttonClicked: (id)sender {
      int tag = sender.tag;
      if(tag == 12) { . . . }
      else if(tag == 13) { . . .}
      else if...
    }
    

    where there is no substantial code in common between the various buttons, then why not simply define a separate method for each button?
    -(IBAction) button12Clicked {  . . . }
    
    -(IBAction) button13Clicked {  . . . }
    

    This takes up less total code, executes faster, and is clearer to document. The selector need not use the "sender" parameter if you do it this way.

    Robert Scott
    Ypsilanti, Michigan
  • Mike JMike J Posts: 110Registered Users
    edited August 2009
    When I enter your code it says that tag is something that isn't in the structure or union.
  • RLScottRLScott Posts: 1,638Tutorial Authors, Registered Users @ @ @ @
    edited August 2009
    Mike J wrote: »
    When I enter your code it says that tag is something that isn't in the structure or union.
    Instead of sender.tag, use [sender tag].
Sign In or Register to comment.