标签未在设置器中更新

发布于 2024-12-24 21:02:36 字数 1111 浏览 3 评论 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 的行。

感谢您的帮助!

干杯, 马特

ok so this problem is kinda weird because the NSLog I have right in front of the line of code that should be printing out the text is returning the correct value.

Here's the code:

-(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 

}

So when the app first launches, everything is fine. The label updates and everything. The problem arises when I hit a button that basically grabs a new date. In the process, it runs this:

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

Again, I'm thinking this should all be correct because the NSLog is returning the correct stuff. I'm thinking that the problem is with the last line in the first block of code I showed... the line with the setText.

thanks for all your help!

cheers,
Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倥絔 2024-12-31 21:02:36

如果您使用笔尖

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

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

因此笔尖将加载并调用 setCurrentDate: 传递在未归档的 UILabel 中作为参数

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

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

您实际上不会存储参考这个ivar 中的 UILabel ,因此从技术上讲,您已经泄漏了标签,并且由于您没有设置 ivar currentDate ,它将被初始化为 nil 。这是用不正确的实现覆盖 setter 的危险。

在您的方法中的某个时刻,您应该将 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]];
}

If you used a nib

When the nib loads and establishes all of it connections it... (From the Resource Programming guide)

looks for a method of the form set OutletName: and calls it if such a method is present

Therefore the nib will load and call setCurrentDate: passing in the unarchived UILabel as the parameter

In your method you configure the UILabel using the local reference passed into the method

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

You at no point actually store a reference to this UILabel in an ivar, so technically you have leaked the label and as you have not set the ivar currentDate it will be initialised to nil. This is the danger of overriding a setter with an incorrect implementation.

At some point in your method you should be setting your ivar to the passed in variable. A normal setter would look like this

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

But

In your example I would not worry about this at all I would instead change this

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

to something like

[self updateCurrentDate];

The implementation would look something like:

- (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]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文