iOS学习之UISwitch控件两种使用方法和监听
来源:互联网 发布:友盟推送 java服务端 编辑:程序博客网 时间:2024/06/07 17:42
一、第一种创建UISwitch控件的方法,在代码中动态创建。
1、打开Xcode 4.3.2, 新建项目Switch,选择Single View Application。
2、打开ViewController.m文件在viewDidLoad方法里添加代码:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 20, 10)];
- [switchButton setOn:YES];
- [switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
- [self.view addSubview:switchButton];
- // Do any additional setup after loading the view, typically from a nib.
- }
[switchButton addTarget:selfaction:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];
代码中selector中的switchAction:需要我们自己实现,就是按下时接收到的事件。记得把switchButton加到当前view,调用[self.viewaddSubview:switchButton];
3、监听UISwitch按下事件
实现代码如下:- -(void)switchAction:(id)sender
- {
- UISwitch *switchButton = (UISwitch*)sender;
- BOOL isButtonOn = [switchButton isOn];
- if (isButtonOn) {
- showSwitchValue.text = @"是";
- }else {
- showSwitchValue.text = @"否";
- }
- }
showSwitchValue是我通过拖拽控件方法放到界面上的Label,方便显示效果
运行,效果:
二、通过拖拽方法使用UISwitch
1、往xib文件上拖拽一个UISwitch控件。
2、按alt+command + return键开启Assistant Editor模式,选中UISwitch控件,按住Control键,往ViewController.h拖拽
3、选Action方式
4、.m文件中实现switchAction 。刚才动态创建的时候也用到这个方法名称,可以先注释掉刚才的。
- - (IBAction)switchAction:(id)sender {
- UISwitch *switchButton = (UISwitch*)sender;
- BOOL isButtonOn = [switchButton isOn];
- if (isButtonOn) {
- showSwitchValue.text = @"是";
- }else {
- showSwitchValue.text = @"否";
- }
1: IOS中的IBOutlet 、IBAction
刚开始学IOS开发,发现ios的mvc架构很清新。
其实这个Controller类就是对应这个View的,而且一般是一对一的关系。
在Controller里面,我们可以声明变量和方法,并可以指定为IBOutlet 或 IBAction。
在Controller里面,我们可以声明变量和方法,并可以指定为IBOutlet 或 IBAction。
0 0
- iOS学习之UISwitch控件两种使用方法和监听
- iOS学习之UISwitch控件两种使用方法和监听
- iOS学习之UISwitch控件两种使用方法和监听
- iOS学习之UISwitch控件两种使用方法和监听
- iOS学习之UISwitch控件两种使用方法和监听
- iOS学习之UISwitch控件两种使用方法和监听
- iOS学习之UISwitch控件两种使用方法
- iOS开发-UI控件:UISwitch控件两种使用方法和监听
- iOS学习之UISwitch控件两种使用方…
- iOS学习之UISwitch控件两种使用方…
- iOS 控件之 UISwitch
- ios基本控件之UISwitch
- UI控件之UISwitch(事件监听)
- iOS UISwitch和UITextField 的监听
- IOS学习笔记之UISwitch
- iOS开发之UISwitch控件的使用
- iOS-UI-基本控件之UISwitch
- iOS-UI-基本控件之UISwitch
- linux网卡phy-mii驱动mdio调试zz150120a
- ajax上传图片TypeError: 'append' called on an object that does not implement interface FormData.
- C语言声明与定义
- android USB OTG功能如何打开及实现
- 把变量和函数的存储类别说清楚也谈Linux内存管理
- iOS学习之UISwitch控件两种使用方法和监听
- android 5.0 编辑彩信时日历和联系人附件不能与其他附件共存
- 在Android4.2中实现bluetooth A2dp Sink(二)——移植Android5.0蓝牙协议栈
- Bluetooth LE(低功耗蓝牙) - 第一部分
- Bluetooth LE(低功耗蓝牙) - 第二部分
- Bluetooth LE(低功耗蓝牙) - 第三部分
- 一维的01背包问题
- JSTL笔记—c标签
- js中 trim()实现