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

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:

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.