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
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

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
    }
  • MichaelEMichaelE Posts: 452Registered 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;112538 said:
    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
    }
  • 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.
  • RLScottRLScott Posts: 1,597Tutorial 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,597Tutorial Authors, Registered Users
    edited August 2009
    Mike J;112808 said:
    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.