添加自定义方法addSlider和sliderChanged
-(IBAction)sliderChanged:(id)sender {
NSLog(@"SliderValue %f",mySlider.value);
}
-(void)addSlider {
mySlider = [[UISlider alloc] initWithFrame:CGRectMake(50, 200, 200, 23)];
[self.view addSubview:mySlider];
mySlider.minimumValue = 10.0;
mySlider.maximumValue = 99.0;
mySlider.continuous = NO;
[mySlider addTarget:self action:@selector(sliderChanged:)
forControlEvents:UIControlEventValueChanged];
}
更新 ViewController.m 中的 viewDidLoad 如下 -
(void)viewDidLoad {
[super viewDidLoad];
[self addSlider];
}