详解iOS App中UISwitch开关组件的基本创建及使用方法
一、第一种创建UISwitch组件的方法,在代码中动态创建。
1、打开Xcode,新建项目Switch,选择SingleViewApplication。
2、打开ViewController.m文件在viewDidLoad方法里添加代码:
(void)viewDidLoad
{
[superviewDidLoad];
UISwitch*switchButton=[[UISwitchalloc]initWithFrame:CGRectMake(50,100,20,10)];
[switchButtonsetOn:YES];
[switchButtonaddTarget:selfaction:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];
[self.viewaddSubview:switchButton];
//Doanyadditionalsetupafterloadingtheview,typicallyfromanib. } [switchButtonaddTarget:selfaction:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];