常见的递增输入,其实是一个 NSTextField 和 NSStepper 组合使用:

简介
stepper 是一个很简单的控件,以至于定义只有这么多:

创建和使用
- (void)addStepper{
NSStepper *stepper = [[NSStepper alloc]initWithFrame:NSMakeRect(100, 100, 100, 100)];
[self.window.contentView addSubview:stepper];
stepper.wantsLayer = YES;
stepper.layer.backgroundColor = [NSColor cyanColor].CGColor;
stepper.minValue = 5;
stepper.maxValue = 10;
stepper.increment = 0.2; //步增值
stepper.valueWraps = NO; //循环,YES - 超过最小值,回到最大值;超过最大值,来到最小值。
stepper.continuous = NO; //默认为YES-用户交互时会立即放松ValueChanged事件,NO 则表示只有等用户交互结束时才放松ValueChanged事件
stepper.autorepeat = YES; //默认为 YES-按住加号或减号不松手,数字会持续变化.continuous = NO 时才有意义。
[stepper setAction:@selector(stepperAction:)];
}
- (void)stepperAction:(id)sender
{
int theValue = [sender intValue];
float fValue = [sender floatValue];
NSLog(@"value : %d , %f",theValue,fValue);
[self.pageSizeTextField setIntValue:theValue];
}
尺寸
如你所见,无论给他设置frame多大,都只显示这么大。
需要大尺寸的 Stepper 的童鞋,可以用两个 button 来自定义。

伊织 2017-09
本文介绍了如何在Mac应用中使用NSStepper控件。包括创建控件、设置属性及响应事件的方法,并提供了完整的示例代码。
992

被折叠的 条评论
为什么被折叠?



