开发者

Why am I getting "Message sent to deallocated instance" in Objective-C?

开发者 https://www.devze.com 2022-12-24 19:51 出处:网络
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

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)];
 开发者_JAVA百科       [button addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        CurrentHouseCount++;


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

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

and use:

controller.HouseID = [(UIButton *)sender tag];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号