一、第一种创建uiswitch组件的方法,在代码中动态创建。
1、打开xcode, 新建项目switch,选择single view application。
2、打开viewcontroller.m文件在viewdidload方法里添加代码:
// do any additional setup after loading the view, typically from a nib.
}
[switchbutton addtarget:selfaction:@selector(switchaction:)forcontrolevents:uicontroleventvaluechanged];
记得把switchbutton加到当前view,调用[self.viewaddsubview:switchbutton];
3、监听uiswitch按下事件
实现代码如下:
运行,效果:
二、通过拖拽方法使用uiswitch
1、往xib文件上拖拽一个uiswitch控件。
2、按alt+command + return键开启assistant editor模式,选中uiswitch控件,按住control键,往viewcontroller.h拖拽
3、选action方式
4、.m文件中实现switchaction 。刚才动态创建的时候也用到这个方法名称,可以先注释掉刚才的。
三、自定义uiswitch
1.使用类别扩展uiswitch。
如下:
下面是uiswitch.h文件:
@interface uiswitch (tagged)
+ (uiswitch *) switchwithlefttext: (nsstring *) tag1 andright: (nsstring *) tag2;
@property (nonatomic, readonly) uilabel *label1;
@property (nonatomic, readonly) uilabel *label2;
@end
#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
((uilabel )[[[[[[isfooorbar subviews] lastobject] subviews] objectatindex:2] subviews]objectatindex:0]).text = @"foo";
((uilabel *)[[[[[[isfooorbar subviews] lastobject] subviews] objectatindex:2] subviews]objectatindex:1]).text = @"bar";*
如对本文有疑问, 点击进行留言回复!!
Codeforces Round #649 (Div. 2) C-Ehab and Prefix MEXs
微信公众号ios和android验签的区别(解决ios验签报错)
IOS系统中使用new Date()报错valid Date
网友评论