【问题标题】:How to check a checkBox has been clicked如何检查复选框已被单击
【发布时间】:2012-04-25 13:23:05
【问题描述】:

我正在尝试使用我以编程方式创建的复选框制作菜单。 以下是我制作复选框的方法:

UIButton* checkBox = [[UIButton alloc] initWithFrame:CGRectMake(60,60,300, 44)]; 
[checkBox setImage:[UIImage imageNamed:@"unmarked.png"] forState:UIControlStateNormal];
[checkBox addTarget:self action:@selector(toggleButton:) forControlEvents: UIControlEventTouchUpInside];
[checkBox setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[self.view addSubview:checkBox];

还有目标:

- (void)toggleButton: (id) sender
{
    checkboxSelected = !checkboxSelected;
    UIButton* check = (UIButton*) sender;
    if (checkboxSelected == NO)
        [check setImage:[UIImage imageNamed:@"unmarked.png"] forState:UIControlStateNormal];
    else
        [check setImage:[UIImage imageNamed:@"marked.png"] forState:UIControlStateNormal];

}

现在我正在制作一个简单的自定义 UIButton,我想检查复选框是否被选中,使用这个自定义 UIButton 和 NSLog 相同,即如果选中了复选框,那么在按钮上单击它应该 NSLog复选框被选中..!

谁能帮我解决这个问题..?

任何帮助将不胜感激。

感谢您的宝贵时间

谢伊莱什

【问题讨论】:

    标签: ios cocoa-touch checkbox uibutton


    【解决方案1】:

    您应该考虑使用UISwitch,因为复选框在苹果移动设备上不是很好。您可以参考教程,例如这里: http://www.xprogress.com/post-30-uiswitch-tutorial-example-how-to-use-the-switch-in-xcode-for-iphone-sample-included/

    或者在这里以编程方式进行: Creating a UISwitch Programmatically

    不过,如果您坚持使用复选框,请看这里: How to create a simple checkbox in iOS?

    【讨论】:

    • 嘿,感谢您的快速响应。我浏览了您提供的链接。但我已经创建了工作正常的复选框。我只是想验证在简单的自定义 UIButton 单击上选中或取消选中复选框。
    • 我不确定您需要什么帮助。所以更多的是关于日志记录?如果是这样,请将NSLog(@"Is checked: %@", checkboxSelected ? @"YES" : @"NO"); 添加到- (void)toggleButton: (id) sender。这将写下按钮的状态。这是你需要的吗?如果没有,请尝试解释更多;)
    • @Ondra Peterka:我可以在 toggleButton 函数中使用 NSLOG 来输出已选中的复选框。但是我正在创建一个菜单,它有 4 个复选框和一个简单的 UIButton“提交”。假设用户选中了 4 个复选框中的 3 个并点击了提交按钮,那么它应该 NSLOG 已选中的 3 个复选框。
    • 我有一个想法使用 BOOL 变量来做到这一点。但不确定它是否会起作用或什么..! :-)
    • 我明白了,所以你可以使用标签。例如在这里查看 stackoverflow.com/questions/7282809/… 。您可以为每个按钮分配一些标签(一些数字),如下所示:checkbutton1.tag =1;checkbutton2.tag = 2; 比在一些 submitButtonClicked 方法(单击提交按钮时运行)检查标签中的值以区分按钮(通过阅读button.tag 值)。这够了吗?
    猜你喜欢
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多