详解iOS App中UISwitch开关组件的基本创建及使用方法

2025-05-29 0 49

一、第一种创建uiswitch组件的方法,在代码中动态创建。

1、打开xcode, 新建项目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,方便显示效果

运行,效果:

详解iOS App中UISwitch开关组件的基本创建及使用方法

二、通过拖拽方法使用uiswitch

1、往xib文件上拖拽一个uiswitch控件。

详解iOS App中UISwitch开关组件的基本创建及使用方法

2、按alt+command + return键开启assistant editor模式,选中uiswitch控件,按住control键,往viewcontroller.h拖拽

详解iOS App中UISwitch开关组件的基本创建及使用方法

3、选action方式

详解iOS App中UISwitch开关组件的基本创建及使用方法

4、.m文件中实现switchaction 。刚才动态创建的时候也用到这个方法名称,可以先注释掉刚才的。

复制代码 代码如下:


(ibaction)switchaction:(id)sender {
uiswitch *switchbutton = (uiswitch*)sender;
bool isbuttonon = [switchbutton ison];
if (isbuttonon) {
showswitchvalue.text = @"是";
}else {
showswitchvalue.text = @"否";
}
}

三、自定义uiswitch

1.使用类别扩展uiswitch。
如下:
下面是uiswitch.h文件:

复制代码 代码如下:


#import

@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";*


四、一些常用方法
获得开关状态

复制代码 代码如下:


bool setting = switchview.ison;
nslog(@"%d",setting);


设置开关状态 no关闭状态,yes打开状态

复制代码 代码如下:


[switchview seton:setting animated:yes];


设置开光的切换

复制代码 代码如下:


switchview.ontintcolor = [uicolor orangecolor];


设置按钮的颜色

复制代码 代码如下:


switchview.thumbtintcolor = [uicolor redcolor];


开关控件边框的颜色

复制代码 代码如下:


switchview.tintcolor = [uicolor purplecolor];


添加触发事件

复制代码 代码如下:


[switchview addtarget:self action:@selector(switchaction:) forcontrolevents:uicontroleventvaluechanged];

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解iOS App中UISwitch开关组件的基本创建及使用方法 https://www.kuaiidc.com/94280.html

相关文章

发表评论
暂无评论