为什么我在Objective-C中收到“Message to deallocated instance”?

时间:2010-03-29 14:04:19

标签: objective-c

我的应用程序上有几个动态创建的按钮。按下时,它们都指向按钮单击事件。当调用按钮按下方法时,发送者的标签(int值)被解析为控制器的房屋ID。它适用于其中一个按钮 - 第一个创建,具体 - 但其他按钮抛出以下错误:

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

我没有在我的代码中的任何位置发布这些按钮。我没有将它们设置为autorelease或类似的东西。我只是想知道为什么他们在点击时这样做。

按钮点击事件:

- (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];   
}

我在创建按钮的位置:

        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++;

1 个答案:

答案 0 :(得分:2)

tag是整数属性,而不是对象。只是设置它

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

并使用:

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