外觀和UISwitch相似 包含+ -按鈕 控制某個值得增,減
UIControl基類
1. Value:
Minimum:最小值 默認為0 設為大于或等于maximumValue時,會報一個NSInvalidArgumentException異常
Maximum:最大值 默認為100 同會報異常
Current:UIStepper控件的當前值,對應于Value
Step:數值變化的補償對應stepValue屬性 默認為1
2.Behavior
Autorepeat(autorepeat):默認為YES-按住加號或減號不松手,數字會持續變化
Continuous(continuous):默認為YES-用戶交互時會立即放松ValueChanged事件,NO則表示只有等用戶交互結束時才放松ValueChanged事件
Wrap(wraps):默認為NO YES-若value加到超過maximumValue將自動轉頭編程minmumValue的值 到minimumValue亦會反轉
定制外觀:
setXxxImage:forState“
setDecrementImage: forState: 定義減號按鈕的圖片
setIncrementImage: forState: 定義加號按鈕的圖片
.h
@property (nonatomic,strong) UIStepper* stepper1;
@property (nonatomic,strong) UIStepper* stepper2;
@property (nonatomic,strong) UIStepper* stepper3;
@property (nonatomic,strong) UITextField* tf1;
@property (nonatomic,strong) UITextField* tf2;
@property (nonatomic,strong) UITextField* tf3;
.m
- (void)viewDidLoad {
[super viewDidLoad];
self.stepper1=[[UIStepper alloc]initWithFrame:CGRectMake(20, 40, 70, 40)];
self.stepper1.tag=1;
[self.stepper1 addTarget:self action:@selector(valuechanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.stepper1];
self.stepper2=[[UIStepper alloc]initWithFrame:CGRectMake(20, 70, 70, 40)];
[self.stepper2 setMinimumValue:5];
[self.stepper2 setMaximumValue:60];
[self.stepper2 setStepValue:5];
self.stepper2.autorepeat=NO;
self.stepper2.continuous=NO;
self.stepper2.tag=2;
[self.stepper2 addTarget:self action:@selector(valuechanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.stepper2];
self.stepper3=[[UIStepper alloc]initWithFrame:CGRectMake(20, 110, 70, 40)];
[self.stepper3 setMinimumValue:10];
[self.stepper3 setMaximumValue:50];
self.stepper3.tag=3;
self.stepper3.wraps=YES;
[self.stepper3 setDecrementImage:[UIImage imageNamed:@"minus.gif"] forState:UIControlStateNormal];
[self.stepper3 setIncrementImage:[UIImage imageNamed:@"plus.gif"] forState:UIControlStateNormal];
[self.stepper3 addTarget:self action:@selector(valuechanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.stepper3];
self.tf1=[[UITextField alloc]initWithFrame:CGRectMake(170, 40, 120, 40)];
[self.view addSubview:self.tf1];
self.tf2=[[UITextField alloc]initWithFrame:CGRectMake(170, 70, 120, 40)];
[self.view addSubview:self.tf2];
self.tf3=[[UITextField alloc]initWithFrame:CGRectMake(170, 110, 120, 40)];
[self.view addSubview:self.tf3];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)valuechanged:(id)sender{
switch ([sender tag]) {
case 1:
self.tf1.text=[NSString stringWithFormat:@"%f", self.stepper1.value];
break;
case 2:
self.tf2.text=[NSString stringWithFormat:@"%f", self.stepper2.value];
break;
case 3:
self.tf3.text=[NSString stringWithFormat:@"%f", self.stepper3.value];
break;
default:
break;
}
}