变量更改时,setter未接收值

时间:2014-08-29 21:11:55

标签: ios objective-c uislider setter

我安排在更改变量时调用setter函数。它从UISlider对象获取值并将其发送到NSInteger变量的setter。当它到达setter代码时,传递的变量等于0.我试图将浮点值(因为它是UISlider.value)强制转换为NSInteger,但它仍然无效。它只是一直显示为0.我希望得到一些帮助。这是一些代码:

@synthesize sleepTimeValue = _sleepTimeValue;

功能:

- (void)setSleepTimeValue:(NSInteger)aSleepTimeValue {
    NSLog(@"\n\
          Sleep Time Value - SETTER CALLED.\n\
          aSleepTimeValue: %ld\n\
          _sleepTimeValue: %ld\n", (long)aSleepTimeValue, (long)_sleepTimeValue);
    if (_sleepTimeValue != aSleepTimeValue) {
        _sleepTimeValue = aSleepTimeValue;
        [[NSNotificationCenter defaultCenter] postNotificationName:kPASetSleepTimeValue object:self];
    }
}

当我更改变量时:

NSLog(@"sleepTimeValue from qualityButtonPressed: %ld\nsliderValueLabel: %ld\nhoursSlider: %ld\n", (long)self.sleepTimeValue, (long)[self.sliderValueLabel.text integerValue], (long)self.hoursSlider.value);
self.sleepQualityValue = selectedIndex+1;
self.sleepTimeValue = (long)self.hoursSlider.value;

在调用setter之前的结果:

hoursSlider: 10

来自setter中的结果:

Sleep Time Value - SETTER CALLED.
aSleepTimeValue: 0
_sleepTimeValue: 0

0 个答案:

没有答案