0

好的,所以这个问题有点奇怪,因为我在应该打印出文本的代码行前面的 NSLog 返回了正确的值。

这是代码:

-(void)setCurrentDate:(UILabel *)currentDate
{

NSInteger onDay = 1; //because if it's today, you are on day one, not zero... no such thing as a day zero

//get the nubmer of days left
if( [[NSUserDefaults standardUserDefaults] objectForKey:@"StartDate"] ){ //if there is something at the userdefaults
    onDay = [self daysToDate:[NSDate date]];
}//otherwise, onDay will just be one

self.theCurrentNumberOfDaysSinceStart = onDay;

NSLog(@"On day: %d", onDay); //this is returning the correct values....

//print it out on the label
[currentDate setText:[NSString stringWithFormat:@"On day: %d", onDay]];//echoes out the current day number 

}

因此,当应用程序首次启动时,一切都很好。标签更新和一切。当我按下一个基本上抓住新日期的按钮时,问题就出现了。在这个过程中,它运行这个:

    //need to reload the "on day" label now
    [self setCurrentDate:self.currentDate];
    //and the "days left" label
    [self setDaysLeft:self.daysLeft];

同样,我认为这一切都应该是正确的,因为 NSLog 正在返回正确的内容。我认为问题出在我展示的第一个代码块中的最后一行......带有 setText 的行。

感谢你的帮助!

干杯,马特

4

1 回答 1

1

如果您使用笔尖

当笔尖加载并建立所有连接时...(来自资源编程指南

查找set OutletName:形式的方法,如果存在这样的方法,则调用它

因此,笔尖将加载并调用setCurrentDate:传入未归档UILabel的参数

在您的方法中,您UILabel使用传递给方法的本地引用来配置

[currentDate setText:[NSString stringWithFormat:@"On day: %d", onDay]];

您实际上在任何时候都不会在 ivar 中存储对此的引用UILabel,因此从技术上讲,您已经泄漏了标签,并且由于您没有设置 ivar currentDate,它将被初始化为nil. 这是用不正确的实现覆盖设置器的危险。

在您的方法中的某个时刻,您应该将 ivar 设置为传入的变量。一个普通的二传手看起来像这样

- (void)setCurrentDate:(UILabel *)currentDate;
{
    if (_currentDate != currentDate) {
        [_currentDate release];
        _currentDate = [currentDate retain];
    }
}

在你的例子中,我根本不会担心这个我会改变这个

//need to reload the "on day" label now
[self setCurrentDate:self.currentDate];

类似于

[self updateCurrentDate];

实现看起来像:

- (void)updateCurrentDate;
{
    NSInteger onDay = 1;

    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"StartDate"]) {
        onDay = [self daysToDate:[NSDate date]];
    }

    self.theCurrentNumberOfDaysSinceStart = onDay;

    [self.currentDate setText:[NSString stringWithFormat:@"On day: %d", onDay]];
}
于 2012-01-02T21:39:38.503 回答