我怎么可能知道用户点击了步进器的哪个按钮(减号或加号按钮)?
- (IBAction)buttonStepper:(id)sender {
int stepperValue = self.outletStepper.value;
self.label.text = [NSString stringWithFormat:@"%d", stepperValue];
}
谢谢:3
您可以观察 steppers value 属性,而不是 addTarget:action,并要求在更改字典中接收旧值和新值
{
UIStepper *stepper = ...;
[stepper addObserver:self forKeyPath:@"value"
options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew
context:0];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == stepper) {
double oldValue = change[NSKeyValueChangeOldKey];
double newValue = change[NSKeyValueChangeNewKey];
double change = newValue - oldValue;
}
}
或子类 UIStepper 并在覆盖的 -setValue 中进行计算:
- (void)viewDidLoad
{
[super viewDidLoad];
oldValue=stepperObj.value;
}
- (IBAction)stepperStep:(id)sender {
if (stepperObj.value>oldValue) {
oldValue=oldValue+1;
NSLog(@"%d",oldValue);
//your code do you want to perform on increment
}
else
{
oldValue=oldValue-1;
NSLog(@"%d",oldValue);
//your code do you want to perform on decrement
}
}
您必须在头文件中将 oldValue 声明为整数...