【问题标题】:Why is my UILabel getting deallocated?为什么我的 UILabel 被释放?
【发布时间】:2015-03-10 18:57:13
【问题描述】:

我正在尝试追查崩溃的根源。我已经分析了该应用程序以寻找僵尸,它似乎与更新我的 UILabel 之一中的文本有关。我认为我没有不正确地设置文本值。这看起来对吗?

这是我设置 UILabel 文本的地方。

- (void) updateTimecodeDisplay
{
    VarController *sharedController = [VarController sharedController];

    int time0 = sharedController.timeCode0Property;
    int time1 = sharedController.timeCode1Property;
    int time2 = sharedController.timeCode2Property;
    int time3 = sharedController.timeCode3Property;
    int time4 = sharedController.timeCode4Property;
    int time5 = sharedController.timeCode5Property;
    int time6 = sharedController.timeCode6Property;
    int time7 = sharedController.timeCode7Property;
    int time8 = sharedController.timeCode8Property;
    int time9 = sharedController.timeCode9Property;


    if (time0 != 16)
    {
        [timeCodeLabel0 setText:[NSString stringWithFormat:@"%d", time0]];
    }
    else {
        [timeCodeLabel0 setText:[NSString stringWithFormat:@""]];
    }
    if (time1 != 16)
    {
       [timeCodeLabel1 setText:[NSString stringWithFormat:@"%d", time1]];
    }
    else {
        [timeCodeLabel1 setText:[NSString stringWithFormat:@""]];
    }
    if (time2 != 16)
    {
        [timeCodeLabel2 setText:[NSString stringWithFormat:@"%d", time2]];
    }
    else {
        [timeCodeLabel2 setText:[NSString stringWithFormat:@""]];
    }
    if (time3 != 16)
    {
        [timeCodeLabel3 setText:[NSString stringWithFormat:@"%d", time3]];
    }
    else {
        [timeCodeLabel3 setText:[NSString stringWithFormat:@""]];
    }
    if (time4 != 16)
    {
        [timeCodeLabel4 setText:[NSString stringWithFormat:@"%d", time4]];
    }
    else {
        [timeCodeLabel4 setText:[NSString stringWithFormat:@""]];
    }
    if (time5 != 16)
    {
        [timeCodeLabel5 setText:[NSString stringWithFormat:@"%d", time5]];
    }
    else {
        [timeCodeLabel5 setText:[NSString stringWithFormat:@""]];
    }
    if (time6 != 16)
    {
        [timeCodeLabel6 setText:[NSString stringWithFormat:@"%d", time6]];
    }
    else {
        [timeCodeLabel6 setText:[NSString stringWithFormat:@""]];
    }
    if (time7 != 16)
    {
        [timeCodeLabel7 setText:[NSString stringWithFormat:@"%d", time7]];
    }
    else {
        [timeCodeLabel7 setText:[NSString stringWithFormat:@""]];
    } 
    if (time8 != 16)
    {
        [timeCodeLabel8 setText:[NSString stringWithFormat:@"%d", time8]];
    }
    else {
        [timeCodeLabel8 setText:[NSString stringWithFormat:@""]];
    }
    if (time9 != 16)
    {
        [timeCodeLabel9 setText:[NSString stringWithFormat:@"%d", time9]];
    }
    else {
        [timeCodeLabel9 setText:[NSString stringWithFormat:@""]];
    }
}

UILabels 在头文件中声明如下:

我现在为 UILabel 添加了如下所示的属性,并将它们合成到 .m 文件中。但仍然遇到完全相同的问题。

@property (weak) IBOutlet UILabel *timeCodeLabel0;
@property (weak) IBOutlet UILabel *timeCodeLabel1;
@property (weak) IBOutlet UILabel *timeCodeLabel2;
@property (weak) IBOutlet UILabel *timeCodeLabel3;
@property (weak) IBOutlet UILabel *timeCodeLabel4;
@property (weak) IBOutlet UILabel *timeCodeLabel5;
@property (weak) IBOutlet UILabel *timeCodeLabel6;
@property (weak) IBOutlet UILabel *timeCodeLabel7;
@property (weak) IBOutlet UILabel *timeCodeLabel8;
@property (weak) IBOutlet UILabel *timeCodeLabel9;

【问题讨论】:

  • 在右侧的堆栈跟踪中,您能看到 updateTimecodeDisplay 中的哪一行被触发了吗?当视图未在屏幕上呈现时是否调用 updateTimecodeDisplay?
  • 你已经在全局声明了 UILabel,而不是作为视图控制器的属性?
  • 请停止破坏你自己的问题,谢谢。

标签: objective-c uilabel automatic-ref-counting nszombie cfstring


【解决方案1】:

我认为你应该看看这篇文章来谈谈“->”运算符:Dot (".") operator and arrow ("->") operator use in C vs. Objective-C

这意味着您不应该使用“->”来访问属性。因为有很多底层。希望这会有所帮助。

IBOutlet 应该使用@weak

【讨论】:

  • 你能发布你如何声明你所有的 UILabel 吗?更多信息可能会有所帮助。
  • 配置文件说它是僵尸。那可能有其他地方导致了这个问题,而问题不在您发布的代码中。
  • IBOutlet 应该使用弱。否则,它将具有保留周期。
  • 您引用 UILabel 的方式也很奇怪。您可以访问点符号或即时变量。但是您只是按名称引用它。我想你可能还有其他问题。您可以尝试调试它而不是运行配置文件吗?我认为你不需要这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-17
  • 1970-01-01
  • 2023-03-28
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 2010-12-18
相关资源
最近更新 更多