ios開關和滑塊控件操作學習

先創建一個單視圖項目;

//

//  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





發佈了32 篇原創文章 · 獲贊 6 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章