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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 10-21-2010, 04:10 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Wien/Austria
Posts: 242
StefanL is on a distinguished road
Default Detect which Button is pressed?

Hi Folks,

I am creating a couple of Buttons dynamically like this:

Code:
 

t = 0;
x = 10;
y = 10;

for (i=0; i < [count intValue]; i++ ){

		 UIImage *imageLoop = [UIImage imageNamed:[[companyName objectAtIndex:i ]stringByAppendingString:@".png"]];
		 
		 NSLog(@"NSMutableArray: %@", [companyName objectAtIndex:i]);
	
		 
		 UIButton* button = [[UIButton alloc] init];
		 
		 [button setImage:imageLoop forState:UIControlStateNormal];
		 [button setImage:imageLoop forState:UIControlStateHighlighted];
		 

		 [button addTarget:self action:@selector(productImagePressed:)
		  forControlEvents:UIControlEventTouchUpInside];
		 button.tag = t;
		 button.frame = CGRectMake(x,y,100,100);
		 [self.view addSubview:button];
		 [button release];
		 
		 x = x + 120;
		 y = y;
		 t = t + 1;
		 
	 }
In my "productImagePressed" Method I am logging like this:

Code:
-(IBAction)productImagePressed:(id)sender {
	
	NSLog(@"Pressed = %@", [companyName objectAtIndex:t]);
	
}
But I am not able to get the correct number - I know that my "t" is wrong, but how can I fetch the pressed button?

Thanks for your Feedback and Advice.
StefanL is offline   Reply With Quote
Old 10-21-2010, 04:23 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Code:
[sender tag]
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-21-2010, 04:41 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Wien/Austria
Posts: 242
StefanL is on a distinguished road
Default

Hi Brian,

thanks!

Code:
-(IBAction)productImagePressed:(id)sender {
	
	UIButton *button = (UIButton*) sender;
	NSLog(@"Clicked button %d", button.tag);
	
}
does it! :-)

BR,

Stefan
StefanL is offline   Reply With Quote
Reply

Bookmarks

Tags
pressed button, uibutton, uiimage

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: 345
11 members and 334 guests
condor304, Desert Diva, Domele, dre, dreamdash3, mottdog, oceanlablight, palme2elie, Paul Slocum, schmallegory
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,659
Threads: 94,118
Posts: 402,895
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dreamdash3
Powered by vBadvanced CMPS v3.1.0

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