先创建一个单视图项目;
//
// HXViewController.h
// switchSlider
//
//
#import <UIKit/UIKit.h>
@interface HXViewController : UIViewController
{
UISwitch *mySwitch;
IBOutlet UILabel *label1;
IBOutlet UILabel *label2;
}
@property (retain,nonatomic)IBOutlet UISwitch *mySwitch;
@property (retain,nonatomic)IBOutlet UILabel *label1;
@property (retain,nonatomic)IBOutlet UILabel *label2;
- (IBAction)handleSwitch:(id)sender;
- (IBAction)handleSlider:(id)sender;
@end
//
// HXViewController.m
// switchSlider
//
/
//
#import "HXViewController.h"
@interface HXViewController ()
@end
@implementation HXViewController
@synthesize mySwitch = _mySwitch;
@synthesize label1 = _label1;
@synthesize label2 = _label2;
- (IBAction)handleSwitch:(id)sender
{
if ([((UISwitch *)sender) isOn] == YES)//开关控件操作
{
NSLog(@"it is on!");
}else
{
NSLog(@"it is off!");
}
}
- (IBAction)handleSlider:(id)sender//滑片控件操作
{
self.label1.text = [NSString stringWithFormat:@"%f",(((UISlider *)sender).value) * 100];
self.label2.text = [NSString stringWithFormat:@"%f",(1 - ((UISlider *)sender).value) * 100];
NSLog(@"value: %f",((UISlider *)sender).value);
if ([(UISlider *)sender value] == ((UISlider *)sender).maximumValue)
{
[mySwitch setOn:YES animated:YES];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[_mySwitch release];
[_label1 release];
[_label2 release];
[super dealloc];
}
@end