如何检查复选框已被单击

时间:2012-04-25 13:23:05

标签: ios cocoa-touch checkbox uibutton

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

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应该检查复选框..!

任何人都可以帮我这个..?

任何帮助都将得到真正的赞赏。

感谢您的时间

SHAILESH

1 个答案:

答案 0 :(得分:0)

您应该考虑使用UISwitch,因为复选框在Apple移动设备上并不是那么好用。你可以参考一个教程,例如这里: 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?