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.
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.
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
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.
Replies
http://www.kelvinkaodev.com
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeSome 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)
Founder, App Apps, LLC
http://app-apps.com
[IMG]http://app-apps.com/press/VideoBot/vi
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomePersonally, I prefer writing code like
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.
http://www.kelvinkaodev.com
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesomewhere there is no substantial code in common between the various buttons, then why not simply define a separate method for each button?
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
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome