外觀和UISwitch相似 包含+ -按鈕 控制某個(gè)值得增悲柱,減
UIControl基類
1. Value:
Minimum:最小值 默認(rèn)為0 設(shè)為大于或等于maximumValue時(shí)抱冷,會(huì)報(bào)一個(gè)NSInvalidArgumentException異常
Maximum:最大值 默認(rèn)為100 同會(huì)報(bào)異常
Current:UIStepper控件的當(dāng)前值银亲,對(duì)應(yīng)于Value
Step:數(shù)值變化的補(bǔ)償對(duì)應(yīng)stepValue屬性 默認(rèn)為1
2.Behavior
Autorepeat(autorepeat):默認(rèn)為YES-按住加號(hào)或減號(hào)不松手檐嚣,數(shù)字會(huì)持續(xù)變化
Continuous(continuous):默認(rèn)為YES-用戶交互時(shí)會(huì)立即放松ValueChanged事件屯碴,NO則表示只有等用戶交互結(jié)束時(shí)才放松ValueChanged事件
Wrap(wraps):默認(rèn)為NO YES-若value加到超過maximumValue將自動(dòng)轉(zhuǎn)頭編程minmumValue的值 到minimumValue亦會(huì)反轉(zhuǎn)
定制外觀:
setXxxImage:forState“
setDecrementImage: forState: 定義減號(hào)按鈕的圖片
setIncrementImage: forState: 定義加號(hào)按鈕的圖片
.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;
}
}