[_UILabelLayer is Hidden]:发送到已解除分配的实例的消息



我有一个UILabel和一个复选框按钮。选中复选框按钮时,显示 uilabel,否则显示标签 5 秒钟并隐藏标签。当我转到其他视图并返回此视图时,我的应用程序崩溃说"[_UILabelLayer隐藏]:发送到已释放实例的消息"

   NSString *display = [NSString stringWithFormat:@"Scanned!nnFormat: %@nnContents:n%@", formatString, result.text];
if(checkbox==NO){
   [self hideLabel:display];
  }
 //method to hide the label for 5 seconds
- (void)hideLabel:(NSString*)text{
self.decodedLabel.hidden=NO;
[self performSelector:@selector(hideLabel) withObject:nil afterDelay:5];//3sec
 }

 -(void)hideLabel{
 self.decodedLabel.hidden= YES;   //app crashed at this point
 }

谁能告诉我为什么会崩溃?

[self performSelector:@selector(hideLabel) withObject:nil afterDelay:5]; 

导致问题。根据我的理解,您正在弹出视图控制器,然后返回它。弹出控制器时,将清除并解除分配所有关联的视图。现在在您的情况下,这发生在 5 秒的范围内。因此,当计时器命中时,它找不到该标签(已经解除分配)。

因此,您可以使用 nstimer 并在屏幕消失后立即将其失效,并在下次屏幕出现时隐藏/取消隐藏标签,具体取决于复选标记状态。

如果这有帮助,请告诉我。

最新更新