iOS应用程序的复选框控件

时间:2012-02-22 07:43:33

标签: ios cocoa-touch checkbox controls

iOS应用程序的对象库中是否有任何复选框控件?我看到的最接近的是开关控制,它可以取一个布尔值。

3 个答案:

答案 0 :(得分:38)

您可以使用UIButton的选定状态,为不同的(通过代码或Interface Builder)设置不同的图像(或文本):

[button setImage:[UIImage imageNamed:@"selected.png"]
        forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:@"unselected.png"]
        forState:UIControlStateNormal];

在内部动作中:

- (IBAction)buttonTouched:(id)sender
{
    button.selected = !button.selected;
    // add other logic
}

答案 1 :(得分:1)

这是我用于屏幕上两个复选框的代码。 (我把它们放在屏幕上的两张图片的底部。我使用UIControlEventTouchDown调用checkBoxTapped方法。这个方法与我的主视图控制器对话,我决定答案是正确还是不正确。然后是主视图控制器回调此视图并告诉它将空白文本框更改为检查:

enter image description here

或x

enter image description here

// Put the Checkboxes on the screen
UIImage *checkBox = [UIImage imageNamed:@"Checkbox"];

self.checkBoxLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxLeft setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxLeft setFrame:checkBoxFrameLeft];
[self.checkBoxLeft addTarget:self 
                      action:@selector(checkBoxTapped:) 
            forControlEvents:UIControlEventTouchDown];

[self.checkBoxLeft setTitle:@"checkBoxLeft" forState:UIControlStateNormal];
self.checkBoxLeft.contentEdgeInsets = insets;
self.checkBoxLeft.showsTouchWhenHighlighted = NO; // Keeps it from turning gray

self.checkBoxRight = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxRight setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxRight setFrame:checkBoxFrameRight];
[self.checkBoxRight addTarget:self 
                       action:@selector(checkBoxTapped:) 
             forControlEvents:UIControlEventTouchDown];

[self.checkBoxRight setTitle:@"checkBoxRight" forState:UIControlStateNormal];
self.checkBoxRight.contentEdgeInsets = insets;
self.checkBoxRight.showsTouchWhenHighlighted = NO; // Keeps it from turning gray

- (void)checkBoxTapped:(UIButton *)buttonPressed {
    [[self delegate] checkBoxTapped:buttonPressed.currentTitle];
}

- (void)highlightCheckbox:(NSString *)checkboxToHighlight withHighlight:(NSString *)highlight {

    if ( [checkboxToHighlight isEqualToString:@"left"] ){
        [self.checkBoxLeft setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
    } else {
        [self.checkBoxRight setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
    }
}

答案 2 :(得分:1)

//定义按钮的属性

声明未经检查的图片

- (void)viewDidLoad
{
    [_checkboxButton setBackgroundImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
}

已检查图片

- (IBAction)buttonTapped:(id)sender
{
    if (_checkboxButton.selected == YES)
    {
        [_checkboxButton setBackgroundImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateSelected];
        _checkboxButton.selected = NO;
    }
    else
    {
        [_checkboxButton setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
        _checkboxButton.selected = YES;
    }
}