UISwitch

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UISwitch *switchTest =[[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 50, 30)];
    switchTest.tag = 10;
//    [switchTest setOn:YES]; //设置开状态
    [switchTest setOn:YES animated:NO];

    [switchTest addTarget:self action:@selector(doTest) forControlEvents:UIControlEventValueChanged];


    [self.view addSubview:switchTest];
}

-(void)doTest
{
//    UISwitch* control = (UISwitch*)sender;
    UISwitch *control = (UISwitch*)[self.view viewWithTag:10];
    if (control.on) {
        NSLog(@"++++ON");
    }
    else {
        NSLog(@"++++OFF");
    }
}


你可能感兴趣的:(UISwitch)