UISwitch——开关控件

  开关的可配置选项很少,一般用于处理布尔值。

  下面给出一个小Demo,创建两个开关,开关一可以控制开关二的可用与否,两者的关系就相当于水闸与水龙头的关系。

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong) UISwitch *swicth1;

@property(nonatomic,strong) UISwitch *switch2;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    UISwitch *swicth1 = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];

    self.swicth1 = swicth1;

    [swicth1 addTarget:self action:@selector(clickSwitch) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:swicth1];

    

    UISwitch *switch2 = [[UISwitch alloc] initWithFrame:CGRectMake(100, 200, 0, 0)];

    self.switch2 = switch2;

    [switch2 addTarget:self action:@selector(clickSwitch) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:switch2];

}

- (void)clickSwitch {

  //switch1控制switch2的可用与否

    self.switch2.userInteractionEnabled = self.swicth1.on;

}

原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/4727447.html