Ios learning notes (3) UISlider and UISwitch controls

Source: Internet
Author: User

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
# Import <UIKit/UIKit. h> @ interface ViewController: UIViewController {UILabel * sliderlabel; UISwitch * leftSwitch; UISwitch * rightSwitch;} @ property (nonatomic, retain) IBOutlet UILabel * sliderlabel; @ property) 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
# 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) shouldAut OrotateToInterfaceOrientation :( 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:

 



From Henry Morgan's column
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.