iOS开发开关
2015-09-24 20:25
441 查看
@interface ViewController ()
@property(strong,nonatomic)UISwitch *switchM;
@property(strong,nonatomic)UIButton *button;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
self.button = [UIButton
new];
self.button.frame =
CGRectMake(20,
20, 100,
30);
self.button.backgroundColor = [UIColor
redColor];
[self.button
setTitle:@"点击我"
forState:UIControlStateNormal];
[self.button
addTarget:self
action:@selector(tapButton:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:self.button];
self.switchM = [[UISwitch
alloc]initWithFrame:CGRectMake(50,
50, 40,
30)];
self.switchM.on =
YES;
[self.switchM
addTarget:self
action:@selector(tapSwitch:)
forControlEvents:UIControlEventValueChanged];
[self.view
addSubview:self.switchM];
}
//需求:开关开着时,按钮可以使用,否则不能使用
- (void)tapSwitch:(UISwitch *)sender
{
//获取开关的值时,使用isOn
if (sender.isOn==YES) {
//enabled控件是否可用
//方法1,设置控件是否可用,.使用enabled属性
//self.button.enabled = YES;
//方法2.设置控件不可用的其他方法
//是否可交互
self.button.userInteractionEnabled =
YES;
}
else
{
//方法1.让控件不可用
// self.button.enabled = NO;
//不可交互的其他方法
//方法2.让控件不可交互
self.button.userInteractionEnabled =
NO;
}
}
- (void)tapButton:(UIButton*)sender
{
NSLog(@"按钮被点击了!");
}
@property(strong,nonatomic)UISwitch *switchM;
@property(strong,nonatomic)UIButton *button;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
self.button = [UIButton
new];
self.button.frame =
CGRectMake(20,
20, 100,
30);
self.button.backgroundColor = [UIColor
redColor];
[self.button
setTitle:@"点击我"
forState:UIControlStateNormal];
[self.button
addTarget:self
action:@selector(tapButton:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:self.button];
self.switchM = [[UISwitch
alloc]initWithFrame:CGRectMake(50,
50, 40,
30)];
self.switchM.on =
YES;
[self.switchM
addTarget:self
action:@selector(tapSwitch:)
forControlEvents:UIControlEventValueChanged];
[self.view
addSubview:self.switchM];
}
//需求:开关开着时,按钮可以使用,否则不能使用
- (void)tapSwitch:(UISwitch *)sender
{
//获取开关的值时,使用isOn
if (sender.isOn==YES) {
//enabled控件是否可用
//方法1,设置控件是否可用,.使用enabled属性
//self.button.enabled = YES;
//方法2.设置控件不可用的其他方法
//是否可交互
self.button.userInteractionEnabled =
YES;
}
else
{
//方法1.让控件不可用
// self.button.enabled = NO;
//不可交互的其他方法
//方法2.让控件不可交互
self.button.userInteractionEnabled =
NO;
}
}
- (void)tapButton:(UIButton*)sender
{
NSLog(@"按钮被点击了!");
}
相关文章推荐
- iOS分段控制器
- iOS开发常用的代码
- iOS开发中那些高效常用的宏
- iOS开发:沙盒机制以及利用沙盒存储字符串、数组、字典等数据
- iOS开发 ----- AFNetWorkingSession
- 作业
- iOS开发:图片的异步加载
- iOS_XML与JSON解析
- IOS Alcatraz Xcode6.4安装指南
- 蓝懿iOS培训日志19 SearchBarController
- 【读书笔记】iOS-NSDate
- 【读书笔记】iOS-NSDate
- iOS Xcode重要配置
- iOS开发1-Hello World
- nagios4.1.1 服务端及监控端部署(一)
- iOS AutoLayout自动布局中级开发教程(4)-label等文字自动适应大小,宽度
- iOS中GCD的相关问题
- IOS开发调用系统相机和打开闪光灯
- IOS Masonry自动布局
- OC属性中setter和getter的实现