iOS学习之UISwitch控件两种使用方…
来源:互联网 发布:90后网络名词 编辑:程序博客网 时间:2024/05/22 15:02
iOS学习之UISwitch控件两种使用方法和监听
分类: iOS开发入门 2012-06-1511:48 7433人阅读 评论(1) 收藏 举报
iosactionxcodeapplication2010
一、第一种创建UISwitch控件的方法,在代码中动态创建。
1、打开Xcode
2、打开ViewController.m文件在viewDidLoad方法里添加代码:
[cpp] viewplaincopy
- -
( void)viewDidLoad - {
-
[super viewDidLoad]; -
UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 20, 10)]; -
[switchButton setOn:YES]; -
[switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChang ed]; -
[self.view addSubview:switchButton]; -
-
// Do any additional setup after loading the view, typically from a nib. - }
[switchButton
记得把switchButton加到当前view,调用[self.viewaddSubview:switchButton];
3、监听UISwitch按下事件
实现代码如下:
[cpp] viewplaincopy
- -(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键开启AssistantEditor模式,选中UISwitch控件,按住Control键,往ViewController.h拖拽
3、选Action方式
4、.m文件中实现switchAction。刚才动态创建的时候也用到这个方法名称,可以先注释掉刚才的。
[cpp] viewplaincopy
- -
(IBAction)switchAction:(id)sender { -
UISwitch *switchButton = (UISwitch*)sender; -
BOOL isButtonOn = [switchButton isOn]; -
if (isButtonOn) { -
showSwitchValue.text = @"是"; -
}else { -
showSwitchValue.text = @"否"; -
} - }
运行就可以了。
- iOS学习之UISwitch控件两种使用方…
- iOS学习之UISwitch控件两种使用方…
- iOS学习之UISwitch控件两种使用方法和监听
- iOS学习之UISwitch控件两种使用方法和监听
- iOS学习之UISwitch控件两种使用方法和监听
- iOS学习之UISwitch控件两种使用方法和监听
- iOS学习之UISwitch控件两种使用方法和监听
- iOS学习之UISwitch控件两种使用方法
- iOS学习之UISwitch控件两种使用方法和监听
- iOS开发之UISwitch控件的使用
- iOS 控件之 UISwitch
- Ios学习之uiswitch的使用
- IOS之UISwitch使用
- ios基本控件之UISwitch
- IOS学习笔记之UISwitch
- iOS空间使用之UISwitch
- iOS开发-UI控件:UISwitch控件两种使用方法和监听
- iOS-UI-基本控件之UISwitch
- 欢迎您在新浪博客安家
- iOS 贪吃蛇的部分设置
- 改变方向
- 推箱子第一关核心代码
- AVAudioPlayer 播放器
- iOS学习之UISwitch控件两种使用方…
- 今天做音乐播放器的时候遇上的问题
- 系统声音服务
- 箱子和人物退后的处理
- [GeeksforGeeks]Remove all adjacent duplicates
- sql语句中left join、right join、inner join的区别
- 推箱子简单的思想编码逻辑
- 实现web版的用户注册
- ios 版得用户登录编写