Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-01-2009, 02:19 AM   #1 (permalink)
New Member
 
Join Date: Feb 2009
Posts: 18
Default how do i know which button was pressed?

i am trying to make a peptide MW calculator.

i have 20 round rect buttons. each is owned by a single Controller NSObject. Each is linked to the Controller through the same IBAction "addAA".

I want:
addAA IBAction to know which button was pressed, read the label from that button, and then lookup the label in my dictionary to do a computation.

I don't know how to do this in an efficient manner. The tutorials I have been working with typically have ONE IBAction for EACH button. This would mean 20 different IBActions each performing the same exact function. Maybe I will have all 20 IBActions link to a third function while passing the button's label?

It'd be a lot of repeated code.

Is there anyway to ask the IBAction which button was pressed that caused it to activate??
peptides is offline   Reply With Quote
Old 02-01-2009, 02:33 AM   #2 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

Simple. Declare your "addAA" method like this:

Code:
- (void)addAA:(id)sender {
    UIButton *btn = (UIButton *)sender;
    // Do stuff with 'btn'
}
RickMaddy is offline   Reply With Quote
Old 02-01-2009, 03:28 AM   #3 (permalink)
New Member
 
Join Date: Feb 2009
Posts: 18
Default

Quote:
Originally Posted by RickMaddy View Post
Simple. Declare your "addAA" method like this:

Code:
- (void)addAA:(id)sender {
    UIButton *btn = (UIButton *)sender;
    // Do stuff with 'btn'
}
Thanks this worked perfectly!
Code:
- (IBAction)addAA:(id)sender {
    UIButton *btn = (UIButton *)sender;
    NSLog(@"The button title is: '%@'", [btn titleForState:UIControlStateNormal]);
}
peptides is offline   Reply With Quote
Old 02-01-2009, 04:27 AM   #4 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Nahariya, Israel
Posts: 8
Default

You can give each button a tag and use it in a switch case block
Code:
- (IBAction) keyTyped:(id)sender{
	int tag = [(UIButton *)sender tag];
neurus is offline   Reply With Quote
Old 02-02-2009, 03:28 AM   #5 (permalink)
New Member
 
Join Date: Feb 2009
Posts: 18
Default

interesting, thanks!
peptides is offline   Reply With Quote
Old 02-07-2009, 10:32 PM   #6 (permalink)
New Member
 
Join Date: Feb 2009
Location: Mississippi
Posts: 30
Default

Quote:
Originally Posted by RickMaddy View Post
Simple. Declare your "addAA" method like this:

Code:
- (void)addAA:(id)sender {
    UIButton *btn = (UIButton *)sender;
    // Do stuff with 'btn'
}
Thanks thats exactly my question. I'm really starting to love this group.
-D
dietrichuhl is offline   Reply With Quote
Reply

Bookmarks

Tags
calculator, controller, ibaction, peptide

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 316
19 members and 297 guests
@sandris, ADY, dacapo, Dani77, djohnson, dre, HDshot, HemiMG, JasonR, MarkC, mer10, nibeck, prchn4christ, ryandb2, spiderguy84, timle8n1, tomtom100, vogueestylee
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:05 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0