iOS开发--UISwitch用法
2015-08-04 14:34
309 查看
原文地址为:http://blog.sina.com.cn/s/blog_4cd8dd130100to3x.html
1.UISwitch的初始化
2.设置UISwitch的初始化状态
3.UISwitch事件的响应
-(void) switchAction:(id)sender
{
UISwitch * switch = (UISwitch *)sender;
}
另个版本:
.h文件中声明:
UISwitch * leftSwitch;
@property (nonatomic , retain) IBOutlet UISwitch
* leftSwitch;
.m文件中添加:
@synthesize leftSwitch;
viewDidUnload()中添加:
leftSwitch = nil;
dealloc()中添加:
[leftSwitch release];
显示:
leftSwitch.hidden = NO;
隐藏:
leftSwitch.hidden = YES;
通过IBAction方法设置开关
-(IBAction) switchChanged:(id)sender
{
UISwitch * whichSwitch = (UISwitch *)sender;
BOOL setting = whichSwitch.isOn;
[leftSwitch setOn:setting animated:YES];
[rightSwitch setOn:setting animated:YES];
}
修改UISwitch的标题,实现自定义UISwitch方法有两种:
1.使用类别扩展UISwitch。
如下:
下面是UISwitch.h文件:
#import <UIKit/UIKit.h>
@interface UISwitch (tagged)
+ (UISwitch *) switchWithLeftText: (NSString *) tag1
andRight: (NSString *) tag2;
@property (nonatomic, readonly) UILabel
*label1;
@property (nonatomic, readonly) UILabel
*label2;
@end
UISwitch.m文件:
#import "UISwitch-Extended.h"
#define TAG_OFFSET 900
@implementation UISwitch (tagged)
- (void) spelunkAndTag: (UIView *) aView withCount:(int *)
count
{
for (UIView *subview in [aView subviews])
{
if ([subview isKindOfClass:[UILabel class]])
{
*count += 1;
[subview setTag:(TAG_OFFSET +
*count)];
}
else
[self spelunkAndTag:subview withCount:count];
}
}
- (UILabel *) label1
{
return (UILabel *)
[self viewWithTag:TAG_OFFSET + 1];
}
- (UILabel *) label2
{
return (UILabel *) [self viewWithTag:TAG_OFFSET + 2];
}
+ (UISwitch *) switchWithLeftText: (NSString *) tag1
andRight: (NSString *) tag2
{
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
int labelCount = 0;
[switchView spelunkAndTag:switchView withCount:&labelCount];
if (labelCount == 2)
{
[switchView.label1 setText:tag1];
[switchView.label2 setText:tag2];
}
return [switchView autorelease];
}
@end
2.还有一种方法,这种方法比较简单,但比较难懂,我不甚理解。
UISwitch *isFooOrBar=[[UISwitch alloc] init];
((UILabel *)[[[[[[isFooOrBar subviews] lastObject] subviews] objectAtIndex:2] subviews]objectAtIndex:0]).text = @"Foo";
((UILabel *)[[[[[[isFooOrBar subviews] lastObject] subviews] objectAtIndex:2] subviews]objectAtIndex:1]).text = @"Bar";
1.UISwitch的初始化
1 | UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(4.0f, 16.0f, 100.0f, 28.0f)]; |
1 | switchView.on = YES ; //设置初始为ON的一边 |
1 | [switchView addTarget: self action: @selector (switchAction:) forControlEvents:UIControlEventValueChanged]; |
{
UISwitch * switch = (UISwitch *)sender;
}
另个版本:
.h文件中声明:
UISwitch * leftSwitch;
@property (nonatomic , retain) IBOutlet UISwitch
* leftSwitch;
.m文件中添加:
@synthesize leftSwitch;
viewDidUnload()中添加:
leftSwitch = nil;
dealloc()中添加:
[leftSwitch release];
显示:
leftSwitch.hidden = NO;
隐藏:
leftSwitch.hidden = YES;
通过IBAction方法设置开关
-(IBAction) switchChanged:(id)sender
{
UISwitch * whichSwitch = (UISwitch *)sender;
BOOL setting = whichSwitch.isOn;
[leftSwitch setOn:setting animated:YES];
[rightSwitch setOn:setting animated:YES];
}
iphone
自定义UISwitch
修改UISwitch的标题,实现自定义UISwitch方法有两种:1.使用类别扩展UISwitch。
如下:
下面是UISwitch.h文件:
#import <UIKit/UIKit.h>
@interface UISwitch (tagged)
+ (UISwitch *) switchWithLeftText: (NSString *) tag1
andRight: (NSString *) tag2;
@property (nonatomic, readonly) UILabel
*label1;
@property (nonatomic, readonly) UILabel
*label2;
@end
UISwitch.m文件:
#import "UISwitch-Extended.h"
#define TAG_OFFSET 900
@implementation UISwitch (tagged)
- (void) spelunkAndTag: (UIView *) aView withCount:(int *)
count
{
for (UIView *subview in [aView subviews])
{
if ([subview isKindOfClass:[UILabel class]])
{
*count += 1;
[subview setTag:(TAG_OFFSET +
*count)];
}
else
[self spelunkAndTag:subview withCount:count];
}
}
- (UILabel *) label1
{
return (UILabel *)
[self viewWithTag:TAG_OFFSET + 1];
}
- (UILabel *) label2
{
return (UILabel *) [self viewWithTag:TAG_OFFSET + 2];
}
+ (UISwitch *) switchWithLeftText: (NSString *) tag1
andRight: (NSString *) tag2
{
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
int labelCount = 0;
[switchView spelunkAndTag:switchView withCount:&labelCount];
if (labelCount == 2)
{
[switchView.label1 setText:tag1];
[switchView.label2 setText:tag2];
}
return [switchView autorelease];
}
@end
2.还有一种方法,这种方法比较简单,但比较难懂,我不甚理解。
UISwitch *isFooOrBar=[[UISwitch alloc] init];
((UILabel *)[[[[[[isFooOrBar subviews] lastObject] subviews] objectAtIndex:2] subviews]objectAtIndex:0]).text = @"Foo";
((UILabel *)[[[[[[isFooOrBar subviews] lastObject] subviews] objectAtIndex:2] subviews]objectAtIndex:1]).text = @"Bar";
相关文章推荐
- UITableView隐藏多余分割线
- 关于UITabelView的右侧Table
- UITableView中的headerView改变颜色
- 基于System V Message queue的PHP消息队列封装
- iOS UITextField总结
- iOS7加载富文本(UILabel)
- Ubuntu 开机出现 grub rescue> 终端模式修复方法
- UIImageJPEGRepresentation和UIImagePNGRepresentation
- UESTC 618 无平方因子数 (容斥 + 莫比乌斯反演)
- Swing-JDialog示例代码-用户登陆UI
- easyUi中的一段漂亮代码之将list转换成tree.
- iOS UIScrollView里面添加UIView,有时候y相差20
- UIView,UILabel,UIButton,
- iOS UI 之 简单概述
- (七十四)Android showDialog时报错requestFeature() must be called before adding content
- 玲珑杯Uinty开发心得——安卓平台发布
- android5.0 必学的UI设计源码实例
- ADF Generator for Dynamic ADF BC and ADF UI
- UI_手势
- 产品经理之UI设计——UI/UX设计趋势