UISwitch开关事件的使用

//

//  ViewController.m

//  UISwitch

//

//  Created by apple on 2021/11/22.

//  Copyright © 2021 apple. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    CGRect rect= CGRectMake(175, 100, 0, 0);

    UISwitch *uiSwitch = [[UISwitch alloc] initWithFrame:rect];

    [uiSwitch setOn:YES animated:YES];

    [uiSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:uiSwitch];

    

}

-(void) switchChanged:(UISwitch*) uiSwitch

{

    NSString *message= @"turn off";

    if(!uiSwitch.isOn){

        message = @"turn on";

    }

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Info" message:message preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];

    [alert addAction:okAction];

    [self presentViewController:alert animated:YES completion:nil];

}


@end