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

查看:23
本文介绍了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 属性并将其连接到它.在下面的代码中,我将其称为fullyPaid",因为我想知道是否有人已全额付款.
  3. 添加以下 2 个函数.'touchesBegan' 函数检查您是否触摸了 'fullyPaid' 标签对象内的某处,如果是,它会调用 'togglePaidStatus' 函数.'togglePaidStatus' 函数设置两个字符串,它们的 unicode 字符分别代表空框 (u2610) 和选中框 (u2611).然后它会比较fullyPaid"对象中当前的内容,并将其与另一个字符串进行切换.

您可能希望在 viewDidLoad 函数中调用 togglePaidStatus 函数以将其初始设置为空字符串.

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天全站免登陆