// 创建:
UIStepper * stepper = [[UIStepper alloc] initWithFrame:CGRectMake(100, 300, 100, 30)];
[self.view addSubview:stepper];
// 一般和输入框联合使用
UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(80, 350, 160, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.tag = 100;
textField.delegate = self;
[self.view addSubview:textField];
// 设置商品个数
int count = arc4random()%10<9?1:5;
textField.text = [NSString stringWithFormat:@”%d”, count];
// 设置计数器
stepper.minimumValue = 1;
stepper.maximumValue = 9;
stepper.value = count;
// 计数器的事件
[stepper addTarget:self action:@selector(dealStepper:) forControlEvents:UIControlEventValueChanged];
}
- (void)dealStepper:(UIStepper *)stepper{
UITextField * textField = (UITextField *)[self.view viewWithTag:100];
textField.text = [NSString stringWithFormat:@”%d”, (int)stepper.value];
NSLog(@”%d”, (int)stepper.value);
}
(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}