UISB 进步器 分栏控制器

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    
    //定义步进器对象
    //按照一定的数值来调整某个数值
    UIStepper* _stepper;
    
    //分栏控制器
    UISegmentedControl* _segControl;
    
    
    
    
}

//控制器属性 对于外部使用
@property(retain,nonatomic)UIStepper* stepper;

@property(retain,nonatomic)UISegmentedControl* segControl;

@end

ViewController.m

#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.
    
    //创建步进器物对象
    _stepper =[[UIStepper alloc]init];
    //设置位置 宽高不能变更
    _stepper.frame=CGRectMake(100, 100, 80, 40);
    
    _stepper.minimumValue=0;
    
    _stepper.maximumValue=100;
    _stepper.value=10;
    //设置步进值 每次向前或向后步进的步伐值
    _stepper.stepValue=1;
    //是否可以重复响应事件操作 按住 自动添加
    _stepper.autorepeat=YES;
    
    //是否将进步结果通过事件函数响应出来
    _stepper.continuous=YES;
    //添加事件函数
    //P1 函数实现体
    //P2 函数体
    //P3 事件值改变状态
    [_stepper addTarget:self action:@selector(stepChange) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:_stepper];
    
    
    //创建分栏控件
    _segControl=[[UISegmentedControl alloc]init];
    _segControl.frame = CGRectMake(10, 200, 300, 40);
    
    //参数 P1 按钮选项文字
    //P2 按钮索引位置
    //P3 是否有动画效果
    
    
    [_segControl insertSegmentWithTitle:@"0元" atIndex:0 animated:NO];
    
    [_segControl insertSegmentWithTitle:@"5元" atIndex:1 animated:NO];
    
    //当前默认按钮的索引
    _segControl.selectedSegmentIndex=0;
    [_segControl addTarget:self action:@selector(segChange) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_segControl];
    
    
    
    
    
    
    
}

-(void)stepChange
{
    NSLog(@"step Change %f",_stepper.value);
    
}

-(void)segChange
{
    NSLog(@"%d",_segControl.selectedSegmentIndex);
    
}

@end
原文地址:https://www.cnblogs.com/zhangqing979797/p/13664231.html