[UIImage retain]:发送给解除分配的实例的消息

时间:2012-05-30 06:01:30

标签: objective-c uiimage

我们在init方法中创建了按钮,我们创建了两个Uiimage对象并创建了Boolean

      if(!isHint)
      {
          [hintBtn setImage:hintBtnImage forState:UIControlStateNormal];
      }
      else
      {
          [hintBtn setImage:hintBtnImagex forState:UIControlStateNormal]; 
      }   
      hintBtn =[[UIButton alloc]initWithFrame:CGRectMake(794, 144, hintBtnImage.size.width, hintBtnImage.size.height)]; 
      [hintBtn setImage:hintBtnImage forState:UIControlStateNormal];
      [hintBtn addTarget:self action:@selector(showHint) forControlEvents:UIControlEventTouchUpInside];

当用户点击tableview单元格时,我们更改布尔变量并设置图像。如下面的代码

    if(wrongx==1)
    {
        NSLog(@"the wrong x is %i",wrongx);    
        isHint = NO;
        [hintBtn setImage:hintBtnImage forState:UIControlStateNormal];
    }  

但是当我第一次点击应用程序它正在工作但第二次我得到以下错误

[UIImage retain]: message sent to deallocated instance

Plz回复我的帖子。谢谢提前

1 个答案:

答案 0 :(得分:0)

你分配了“hintBtnImage”对象。我认为你试图在没有分配的情况下访问“hintBtnImage”。

hintBtn =[[UIButton alloc]initWithFrame:CGRectMake(794, 144, hintBtnImage.size.width, hintBtnImage.size.height)];

需要进行小代码修改,其与解决问题无关,上面的行应该是这样的

hintBtn =[[UIButton alloc]initWithFrame:CGRectMake(794, 144, hintBtnImage.size.width, hintBtnImage.size.height)];
      if(!isHint)
      {
          [hintBtn setImage:hintBtnImage forState:UIControlStateNormal];
      }
      else
      {
          [hintBtn setImage:hintBtnImagex forState:UIControlStateNormal]; 
      }