tags:

views:

229

answers:

1

I have several buttons on my app that are being created dynamically. They are all pointed at the button click event when pressed. When the button pressed method is called, the sender's tag (int value) is parsed into the controller's house ID. It works with one of the buttons — the first one created, to be specific — but the others throw the following error:

-[CFNumber intValue]: message sent to deallocated instance 0xc4bb0ff0

I am not releasing these buttons anywhere in my code. I haven't set them to autorelease or anything like that. I'm just wondering why they are doing this on the click.

The button click event:

- (IBAction) ButtonClick: (id) sender
{

    HouseholdViewController *controller = [[HouseholdViewController alloc] initWithNibName:@"HouseholdViewController" bundle:nil];
    controller.delegate = self;

    controller.HouseID = [(NSInteger)[(UIButton *)sender tag] intValue]; //this line throws an error

    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];

    [controller release];   
}

Where I am creating the buttons:

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(MyLongInScreenCoords, MyLatInScreenCoords, 50, 50);
        UIImage *buttonImageNormal = [UIImage imageNamed:@"blue_pin.png"];
        UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:50 topCapHeight:50];
        [button setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
        [self.view  addSubview:button];
        button.tag = [NSNumber numberWithInt:[[words objectAtIndex: i] intValue]];
        ButtonPoints[CurrentHouseCount][0] = button;
        ButtonPoints[CurrentHouseCount][1] = [NSValue valueWithCGPoint:CGPointMake(MyActualLat, MyActualLong)];
        [button addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        CurrentHouseCount++;
+2  A: 

tag is an integer property, not an object. just set it

button.tag = [[words objectAtIndex: i] intValue];

and use:

controller.HouseID = [(UIButton *)sender tag];
Vladimir
Thank you for your response, I did not know that this was an int type and not an object. I kinda wish the ide was like visual studios and gave me more information about objects.
Dave C
@Dave: You can Alt-double click to reach the documentation.
KennyTM
http://stackoverflow.com/questions/146297/what-are-those-little-xcode-tips-tricks-you-wish-you-knew-about-2-years-ago this thread may be helpfull to get along with xcode
Vladimir