1. First, create a single view application, open mainstoryboard_iphone.storyboard, and add a uislider control and a label in Ib. This label is used to display the slider value.
Select the newly added slider control, open attribute inspector, modify the attribute value, set the minimum value to 0, the maximum value to 100, and the current value to 0.5, and make sure to check continuous, such:
Then we put the uiswitch control, which is similar to the switch control. It has only two States: On and off. The effect is as follows:
2. Now let's start writing code Hello: viewcontroller. h:
#import <UIKit/UIKit.h>@interface ViewController : UIViewController{ UILabel *sliderlabel; UISwitch *leftSwitch; UISwitch *rightSwitch;}@property (nonatomic,retain) IBOutlet UILabel *sliderlabel;@property (nonatomic,retain) IBOutlet UISwitch *leftSwitch;@property (nonatomic,retain) IBOutlet UISwitch *rightSwitch;- (IBAction)sliderChanged:(id)sender;- (IBAction)switchChanged:(id)sender;@end
Then we implement viewcontroller. M:
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize sliderlabel;@synthesize leftSwitch;@synthesize rightSwitch;- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}- (void)viewDidUnload{ [super viewDidUnload]; // Release any retained subviews of the main view.}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } else { return YES; }}- (IBAction)sliderChanged:(id)sender{ UISlider *slider=(UISlider*)sender; int progressAsInt=(int)(slider.value+0.5f); NSString *newText=[[NSString alloc] initWithFormat:@"%d",progressAsInt]; sliderlabel.text=newText; [newText release]; NSLog(@"%d",progressAsInt);}- (IBAction)switchChanged:(id)sender{ UISwitch *whichSwich=(UISwitch *)sender; BOOL setting=whichSwich.isOn; [leftSwitch setOn:setting animated:YES]; [rightSwitch setOn:setting animated:YES];}- (void)dealloc{ [sliderlabel release]; [leftSwitch release]; [rightSwitch release]; [super dealloc];}@end
3. The remaining are connection operations and output ports:
Connect the value changed event of the slider control with the sliderchanged method, and connect the value changed event of the swich control with the swichchanged method, of course, the output of the lable control and swich control must be connected with the corresponding control interface of viewcontroller.
The final effect is shown in the following two figures: