Wednesday, May 15, 2013

UISlider and UISwitch controls http://www.iphonepicks.com/the-ios-study-notes-c-the-uislider-and-uiswitch-controls/

http://www.iphonepicks.com/the-ios-study-notes-c-the-uislider-and-uiswitch-controls/

the ios study notes (c) the UISlider and UISwitch controls


First we create a Single View Application, and then open theMainStoryboard_iphone.storyboardIn IB, add a UISlider control and a Label, Label is used to display the value of the Slider.


Select the new added Slider control, open the Attribute Inspector, modify the property value, the minimum value is set to 0, the maximum value of 100, the current value of 0.5, and to ensure a check on Continuous, as shown below:



Then we put a UISwitch controls, is much like the switch that controls only two states: on and off, all put up effect is this:






2 we begin to write code myself: ViewController.h the:


  #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 realized 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


Remaining is connected operation and output ports:


Slider control value changed the event with sliderChanged method to connect, connected together swich control value changed the event with swichChanged method, of course, but also the lable control and swich control output the ViewController corresponding control interface to connect together.


Ultimately achieve the effect, such as the following two graphs:

No comments:

Post a Comment