iOS应用程序中的复选框 [英] Checkbox in iOS application

查看:144
本文介绍了iOS应用程序中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要向我的表单添加复选框控件。我知道在iOS SDK中没有这样的控制。我如何做到这一点?

I need to add checkbox controls to my form. I know that there is no such control in iOS SDK. How could I do this?

推荐答案

这一直推动我疯了,我发现一个不同的解决方案,

this has been driving me mad too and I found a different solution that works well for me and avoids having to use images.


  1. 在Interface Builder中添加新的标签对象。

  2. 创建一个在Xcode中的IBOutlet属性并将其连接到它。在下面的代码中,我称之为完全付费,因为我想知道某人是否已经完全支付了一笔钱。

  3. 添加以下2个功能。
    'touchesBegan'函数检查是否在'fullyPaid'标签对象内部某处触摸,如果是,则调用'togglePaidStatus'函数。
    togglePaidStatus函数设置两个字符串,其中unicode字符分别表示一个空框(\\\☐)和一个复选框(\\\☑)。然后比较fullyPaid对象中的内容,并将其与其他字符串切换。

您可能想调用togglePaidStatus函数在viewDidLoad函数中将它设置为一个空字符串。

You might want to call the togglePaidStatus function in the viewDidLoad function to set it to an empty string initially.

显然,你可以添加额外的检查,以防止用户在标签未启用时切换复选框,如下所示。

Obviously you can add extra checks to prevent users toggling the checkbox if the label is not enabled, but that's not shown below.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];   
    if (CGRectContainsPoint([fullyPaid frame], [touch locationInView:self.view]))
    {
        [self togglePaidStatus];
    }
}
-(void) togglePaidStatus
{
    NSString *untickedBoxStr = [[NSString alloc] initWithString:@"\u2610"];
    NSString *tickedBoxStr = [[NSString alloc] initWithString:@"\u2611"];   

    if ([fullyPaid.text isEqualToString:tickedBoxStr])
    {
        fullyPaid.text = untickedBoxStr;
    }
    else
    {
        fullyPaid.text = tickedBoxStr;
    }

    [tickedBoxStr release];
    [untickedBoxStr release];
}

这篇关于iOS应用程序中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆