加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

UI类 - UISwitch的使用

(2013-04-18 14:57:10)
标签:

uiswitch

it

分类: Mac/IOS那些事
|--UISwitchViewController.h

#import


@interface UISwitchViewController : UIViewController

{

    UISwitch* _oneSwitch;

    UISwitch* _twoSwith;

}

@property (retain,nonatomic) UISwitch* oneSwitch;

@property (retain,nonatomic) UISwitch* twoSwitch;

-(IBAction)switchChange:(id)sender;          //定义连个switch一个当switch时候调用的事件

@end

|--UISwitchViewController.m

 

#import "UISwitchViewController.h"


@interface UISwitchViewController ()


@end


@implementation UISwitchViewController

@synthesize oneSwitch=_oneSwitch,twoSwitch=_twoSwith;


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    _oneSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(100, 50, 40, 40)];//创建两个switch

    _twoSwith = [[UISwitch alloc]initWithFrame:CGRectMake(100, 200, 40, 40)];

    [_oneSwitch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];                    //定义调用的事件和动作

    [self.view addSubview:_oneSwitch]; //加到self.view显示

    [_twoSwith addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:_twoSwith];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(IBAction)switchChange:(id)sender

{

    //传过来的参数转为uiswitch

    UISwitch* testSwitch = (UISwitch *)sender;

    BOOL isOn = testSwitch.isOn;   //给一个定义的值 活的开关状态

    if (isOn) {

        NSLog(@"switchOn");

    } else {

        NSLog(@"switchOff");

    }

    [_oneSwitch setOn:isOn animated:YES];//设置属性和动作

    [_twoSwith setOn:isOn animated:YES];

}


@end


0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有