在线Stanford CS193p iPhone Application Development课程第6讲,构建了一个应用程序,它有一个滑块作为输入,一个自定义视图作为输出。
更改滑块后,视图控制器会再次设置滑块值。
Happiness 2.zip中视图控制器的重要位:
@implementation HappinessViewController
@synthesize happiness;
- (void)updateUI
{
// assignment-loop when called from happinessChanged:?
self.slider.value = self.happiness; // sets slider to model's (corrected) value
[self.faceView setNeedsDisplay];
}
- (void)setHappiness:(int)newHappiness
{
if (newHappiness < 0) newHappiness = 0; // limit value
if (newHappiness > 100) newHappiness = 100;
happiness = newHappiness;
[self updateUI]; // changed happiness should update view
}
- (IBAction)happinessChanged:(UISlider *)sender // called by changed slider
{
self.happiness = sender.value; // calls setter setHappiness:
}
这不会导致循环(滑块更改 - &gt;模型更新 - &gt;更改滑块 - &gt;?)?
或者这是不错的做法?
答案 0 :(得分:1)
如果滑块是从代码而不是用户更新的,则可能不会发送valueChanged操作。所以你没有得到无限循环。
这可用于“校正”用户选择的值,或强制滑块使用常规刻度而不是平滑刻度。