#import <UIKit/UIKit.h>
@interface ControlViewController : UIViewController {
UILabel *sliderLabel;
}
@property (nonatomic,retain) IBOutlet UILabel *sliderLabel;
-(IBAction)sliderChanged:(id)sender;
@end

@implementation ControlViewController
@synthesize sliderLabel;
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

-(IBAction)sliderChanged:(id)sender
{
//首先将sender装换承UISlider,作用是让代码可读性变强,
//并避免每次使用sender都对他进行其它类型转换。
UISlider *slider = (UISlider *)sender;
//接收滑块的值,将其加0.5,以便四舍五入为整型值。
int progresAsInt = (int)(slider.value +0.5f);
NSString *newText = [[NSString alloc] initWithFormat:@"%d",progresAsInt];
sliderLabel.text = newText;
[newText release];
}
- (void)dealloc {
[sliderLabel release];
[super dealloc];
}
@end


进入Interface Builder,加入Label和Slider两个组件。并分别进行设置:


[img]http://dl.iteye.com/upload/attachment/0065/9307/fe212e92-3e3d-3760-830d-a83429dd8bbf.png[/img]


Label:


[img]http://dl.iteye.com/upload/attachment/0065/9309/c4f37765-2244-3761-bb4a-adc897ffe2d2.png[/img]


Slider:


[img]http://dl.iteye.com/upload/attachment/0065/9317/2c464ac3-7280-3951-9362-7f4f8c5b3710.png[/img]


[img]http://dl.iteye.com/upload/attachment/0065/9319/ccf4bc63-2586-3db8-8811-a5ddd95ed0ef.png[/img]


绑定sliderLabel:


[img]http://dl.iteye.com/upload/attachment/0065/9321/e2e30b09-b0f3-3d02-9136-4a32d5636bc8.png[/img]


绑定ValueChanged事件到sliderChanged操作:


[img]http://dl.iteye.com/upload/attachment/0065/9323/4df453d9-c026-369b-8e3c-7cc6f6aa5124.png[/img]



NSLog("Success!!");