Switch用于在打开和关闭状态之间切换。
- (void)setOn:(BOOL)on animated:(BOOL)animated
-(IBAction)switched:(id)sender { NSLog(@"Switch current state %@", mySwitch.on ? @"On" : @"Off"); } -(void)addSwitch { mySwitch = [[UISwitch alloc] init]; [self.view addSubview:mySwitch]; mySwitch.center = CGPointMake(150, 200); [mySwitch addTarget:self action:@selector(switched:) forControlEvents:UIControlEventValueChanged]; }
更新ViewController.m中的viewDidLoad,如下所示:
(void)viewDidLoad { [super viewDidLoad]; [self addSwitch]; }
运行应用程序时,将获得以下输出-
链接:https://www.learnfk.comhttps://www.learnfk.com/ios/ios-ui-elements-switches.html
来源:LearnFk无涯教程网
向右滑动Switch时,输出如下-
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)