ViewController.h
// 步进器&分栏控件
//
// Created by mac on 2016/10/28.
// Copyright © 2016年 mac. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
// 定义步进器对象
// 按照一定的数字来调整某个数据
UIStepper* _stepper;
// 分栏控制器
UISegmentedControl* _segControl;
}
@property (retain , nonatomic ) UIStepper* stepper;
@property (retain , nonatomic ) UISegmentedControl* segControl;
@end
ViewController.m
// 步进器&分栏控件
//
// Created by mac on 2016/10/28.
// Copyright © 2016年 mac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize stepper =_stepper;
@synthesize segControl = _segControl;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 创建步进器对象
_stepper = [[UIStepper alloc] init];
// 设置位置,宽高不能改变
_stepper.frame = CGRectMake(100, 100, 100, 100);
// 设置步进器的最小值
_stepper.minimumValue = 0;
// 设置步进器的最大值
_stepper.maximumValue = 10;
// 设置步进器的当前值,默认值为0
_stepper.value = 10;
// 设置步进值,每次向前或者向后的步伐值
_stepper.stepValue = 1;
// 是否可以重复响应事件操作
// 意思就是按住按钮的时候是否连续的响应
_stepper.autorepeat = YES;
// 是否将步进结果通过事件函数响应出来
// 值为NO的时候按住按钮松开的时候只响应最后一步
_stepper.continuous = YES;
// 添加事件函数
// 1:函数实现体
// 2:函数体
// 3:事件改变状态
[_stepper addTarget:self action:@selector(stepChange) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_stepper];
// 创建分栏控件
_segControl = [[UISegmentedControl alloc] init];
// 设置位置大小宽度可以变,高度不可以变
_segControl.frame = CGRectMake(10, 200, 300, 40);
// 添加一个按钮元素
[_segControl insertSegmentWithTitle:@"0元" atIndex:0 animated:NO];
// p1:按钮选项文字
// p2:按钮的索引位置
// p3:是否有插入的动画效果
[_segControl insertSegmentWithTitle:@"5元" atIndex:1 animated:NO];
[_segControl insertSegmentWithTitle:@"10元" atIndex:2 animated:NO];
[_segControl insertSegmentWithTitle:@"30元" atIndex:2 animated:NO];
[_segControl insertSegmentWithTitle:@"50元" atIndex:2 animated:NO];
// 当前默认按钮索引设置
_segControl.selectedSegmentIndex = 0;
[_segControl addTarget:self action:@selector(segChange) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_segControl];
}
-(void) stepChange
{
NSLog(@"step press value =%f",_stepper.value);
}
-(void) segChange
{
NSLog(@"%ld",_segControl.selectedSegmentIndex);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end