标签未在设置器中更新
好吧,这个问题有点奇怪,因为我在应该打印文本的代码行前面的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用笔尖
当笔尖加载并建立所有连接时...(来自 资源编程指南)
因此笔尖将加载并调用
setCurrentDate:
传递在未归档的UILabel
中作为参数在您的方法中,您使用传递到方法中的本地引用来配置
UILabel
您实际上不会存储参考这个ivar 中的
UILabel
,因此从技术上讲,您已经泄漏了标签,并且由于您没有设置 ivarcurrentDate
,它将被初始化为nil
。这是用不正确的实现覆盖 setter 的危险。在您的方法中的某个时刻,您应该将 ivar 设置为传入的变量。普通的设置器看起来像这样
但是
在你的例子中我根本不担心这个我会把它改为
类似的
实现看起来像这样:
If you used a nib
When the nib loads and establishes all of it connections it... (From the Resource Programming guide)
Therefore the nib will load and call
setCurrentDate:
passing in the unarchivedUILabel
as the parameterIn your method you configure the
UILabel
using the local reference passed into the methodYou 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 ivarcurrentDate
it will be initialised tonil
. 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
But
In your example I would not worry about this at all I would instead change this
to something like
The implementation would look something like: