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 > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-03-2011, 06:18 PM   #1 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 551
aldcorn@live.com is on a distinguished road
Default Figure out which button was fired?

Ok,
I hope that I can explain my question properly. I have an number of uiviews that are created programmatically. Each of these views contain a uibutton and a uilabel as subviews.

The uiviews that I create are added to an nsmutable array and then displayed in a uiviewcontroller. All of this is working the way I like; however I am adding a target action to the buttons ( which eventually become subviews of the uiviews that are added to the array) and as of now I am sucessfully firing the actions when any of the buttons are touched. The problem is that it is the same action regardless of the button that is pressed.

What I would like to do is fire a different action for each of the buttons. What would be a good approach to achieve this?

My guess: Each uiview only has one button so I need to either determine which view contained the button pressed or somehow identify which specific button was pressed. Can I give each button or each view an unique identifier when it is created? Then add if statements to my action?

I will run to my computer and post some code in a second.
aldcorn@live.com is offline   Reply With Quote
Old 11-03-2011, 06:20 PM   #2 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 551
aldcorn@live.com is on a distinguished road
Default The current code

Code:
{
	[super viewDidLoad];
	ARView *arView = (ARView *)self.view;
	
	// Create array of hard-coded places-of-interest, in this case some famous parks
    const char *poiNames[] = {"ONE",
                              "TWO",
                              "THREE",
                              "FOUR",
                              "FIVE",
                              "SIX"};
	
    CLLocationCoordinate2D poiCoords[] = {{40.7711329, -73.9741874},
                                          {37.7690400, -122.4835193},
                                          {32.7343822, -117.1441227},
                                          {51.5068670, -0.1708030},
                                          {45.5126399, -73.6802448},
                                          {40.4152519, -3.6887466}};
                                          
    int numPois = sizeof(poiCoords) / sizeof(CLLocationCoordinate2D);
		
	NSMutableArray *placesOfInterest = [NSMutableArray arrayWithCapacity:numPois];
	for (int i = 0; i < numPois; i++) {
        
        UILabel *label = [[[UILabel alloc] init] autorelease];
		label.adjustsFontSizeToFitWidth = NO;
		label.opaque = NO;
		label.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.5f];

		label.textAlignment = UITextAlignmentCenter;
		label.textColor = [UIColor whiteColor];
		label.text = [NSString stringWithCString:poiNames[i] encoding:NSASCIIStringEncoding];		
		CGSize size = [label.text sizeWithFont:label.font];
		label.bounds = CGRectMake(20.0f, 20.0f, size.width, size.height);
        
        UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 20.0f, 100.0f)] autorelease];
        [button setBackgroundColor:[UIColor redColor]];
        [button addTarget:self action:@selector(buttonPushed:)
              forControlEvents:UIControlEventTouchUpInside];
        
        UIView *viewMine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 120.0f)];
            [viewMine setBackgroundColor:[UIColor blueColor]];
        [viewMine addSubview:label];
        [viewMine addSubview:button];
        
        PlaceOfInterest *poi = [PlaceOfInterest placeOfInterestWithView:viewMine at:[[[CLLocation alloc] initWithLatitude:poiCoords[i].latitude longitude:poiCoords[i].longitude] autorelease]];
		[placesOfInterest insertObject:poi atIndex:i];

	}	
	[arView setPlacesOfInterest:placesOfInterest];	
}
- (void) buttonPushed:(id)button {  
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"0" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
}
So in the buttonPushed method how would I check to see which button or view containing the button was touched?

Thanks for your time,
Rob

Last edited by aldcorn@live.com; 11-03-2011 at 06:22 PM. Reason: typo
aldcorn@live.com is offline   Reply With Quote
Old 11-03-2011, 06:38 PM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 551
aldcorn@live.com is on a distinguished road
Default Posted in the wrong category sorry

Can this post be moved to the iOS section?
aldcorn@live.com is offline   Reply With Quote
Old 11-04-2011, 01:15 AM   #4 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 551
aldcorn@live.com is on a distinguished road
Default Figured it out! Used tags.

Added two things:
I assigned a tag to each button as it was created using i:
Code:
[button setTag:i];
I adjusted the buttonPushed action:
Code:
- (void) buttonPushed:(id)sender { 
    UIButton *button = (UIButton *)sender;
    if ([button tag] == 4) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"tag 4" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"not tag 4" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
    }
}
Full corrected code below:
Code:
- (void)viewDidLoad
{
	[super viewDidLoad];
	ARView *arView = (ARView *)self.view;
	
	// Create array of hard-coded places-of-interest, in this case some famous parks
    const char *poiNames[] = {"ONE",
                              "TWO",
                              "THREE",
                              "FOUR",
                              "FIVE",
                              "SIX"};
	
    CLLocationCoordinate2D poiCoords[] = {{40.7711329, -73.9741874},
                                          {37.7690400, -122.4835193},
                                          {32.7343822, -117.1441227},
                                          {51.5068670, -0.1708030},
                                          {45.5126399, -73.6802448},
                                          {40.4152519, -3.6887466}};
                                          
    int numPois = sizeof(poiCoords) / sizeof(CLLocationCoordinate2D);
		
	NSMutableArray *placesOfInterest = [NSMutableArray arrayWithCapacity:numPois];
	for (int i = 0; i < numPois; i++) {
        
        UILabel *label = [[[UILabel alloc] init] autorelease];
		label.adjustsFontSizeToFitWidth = NO;
		label.opaque = NO;
		label.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.5f];

		label.textAlignment = UITextAlignmentCenter;
		label.textColor = [UIColor whiteColor];
		label.text = [NSString stringWithCString:poiNames[i] encoding:NSASCIIStringEncoding];		
		CGSize size = [label.text sizeWithFont:label.font];
		label.bounds = CGRectMake(20.0f, 20.0f, size.width, size.height);
        
        UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)] autorelease];
        [button setBackgroundColor:[UIColor redColor]];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitle:[NSString stringWithCString:poiNames[i] encoding:NSASCIIStringEncoding] forState:UIControlStateNormal];
        [button setTag:i];
        
        [button addTarget:self action:@selector(buttonPushed:)
              forControlEvents:UIControlEventTouchUpInside];
        
        UIView *viewMine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 120.0f)];
            [viewMine setBackgroundColor:[UIColor blueColor]];
        [viewMine addSubview:label];
        [viewMine addSubview:button];
        
        PlaceOfInterest *poi = [PlaceOfInterest placeOfInterestWithView:viewMine at:[[[CLLocation alloc] initWithLatitude:poiCoords[i].latitude longitude:poiCoords[i].longitude] autorelease]];
		[placesOfInterest insertObject:poi atIndex:i];

	}	
	[arView setPlacesOfInterest:placesOfInterest];	
}
- (void) buttonPushed:(id)sender { 
    UIButton *button = (UIButton *)sender;
    if ([button tag] == 4) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"tag 4" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"not tag 4" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
    }
}
aldcorn@live.com is offline   Reply With Quote
Reply

Bookmarks

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: 491
14 members and 477 guests
AlanFloyd, andyzaharia, AppsBlogger, David-T, HemiMG, iAppDeveloper, imac74, Jaxen66, lovoyl, mutantskin, Paul Slocum, tiendung2992, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,684
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, tiendung2992
Powered by vBadvanced CMPS v3.1.0

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