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

有帮助吗?

解决方案

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]];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top