直接上代码喽:
.h
@interface ViewController : UIViewController { //声明步进器对象 UIStepper *_stepper; } //声明属性 @property (nonatomic, strong) UIStepper *stepper; @end
.m
#import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UILabel *label; @end @implementation ViewController //同步属性和成员变量 @synthesize stepper = _stepper; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; //创建步进器对象 UIStepper *sterper = [[UIStepper alloc]init]; //设置Frame(大小为系统内设定); sterper.frame = CGRectMake(100, 100, 0, 0); //设置每次增加的数量 sterper.stepValue = 1; //设置最小值(默认为0) sterper.minimumValue = 0; //设置最大值(默认为100) sterper.maximumValue = 10; //从小到大(默认为NO) sterper.wraps = YES; //设置是否重复(默认为YES) sterper.autorepeat = YES; //是否连续(默认为YES) sterper.continuous = YES; //添加到当前视图上 [self.view addSubview:sterper]; self.stepper = sterper; /** 创建Label,将值显示在Label上; */ UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 40, 100, 30)]; label.backgroundColor = [UIColor lightGrayColor]; label.textColor = [UIColor purpleColor]; label.textAlignment = 1; [self.view addSubview:label]; self.label = label; //添加步进器点击事件 [sterper addTarget:self action:@selector(addCount:) forControlEvents:UIControlEventValueChanged]; } //实现步进器事件 - (void)addCount:(UIStepper *)steper{ self.label.text = [NSString stringWithFormat:@"%.0f", steper.value]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end