UIStepper控件详解

来源:互联网 发布:红旗linux系统 编辑:程序博客网 时间:2024/06/04 18:46

UIStepper继承自UIControl,它主要的事件是UIControlEventValueChanged,每当它的值改变了就会触发这个事件。

它主要有下面几个属性

1、value 当前所表示的值,默认0.0

2、minimumValue 最小可以表示的值,默认0.0

3、maximumValue 最大可以表示的值,默认100.0

4、stepValue 每次递增或递减的值,默认1.0


#import "AppDelegate.h"


@interface AppDelegate () {
    UILabel *label;//设置全局变量

}


@end


@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

   

    //创建label,实现数字变化,为方便使用,将label设置为全局变量

    label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 100, 30)];
    label.text = @"0";//初始值从0开始变化
    //label.backgroundColor = [UIColor greenColor];
    [self.window addSubview:label];
    [label release];
    
    UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    stepper.minimumValue = 0;//最小值
    stepper.maximumValue = 100;//最大值

    stepper.stepValue = 100;//单次点击变化量

    //为stepper关联一个方法,实现label的数值变化

    [stepper addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:stepper];
    [stepper release];
 return YES;
}
-(void)change:(UIStepper *)stepper {
   label.text = [NSString stringWithFormat:@"%.2lf", stepper.value];
}
@end
0 0
原创粉丝点击