【问题标题】:Checkbox control for iOS applicationiOS 应用程序的复选框控件
【发布时间】:2012-02-22 07:43:33
【问题描述】:

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

【问题讨论】:

  • UISegmentedControl 可以帮到你
  • 不,复选框或单选按钮没有默认的 UI 控件,在这里你必须使用 2 个不同的图像进行选中和未选中并在逻辑上控制它们
  • 将 UIButton 状态用于选中/取消选中标记。放上未选中(正常状态)和选中(选中状态)的图片。

标签: ios cocoa-touch checkbox controls


【解决方案1】:

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

[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
}

【讨论】:

  • 这是一个提供实现的开源库。 github.com/t4ku/RadioButtonWithUIKit
  • Anirudha - 你可以设置背景图片,这可能就是你所说的“不起作用”。图像是按钮上的图像,就像标签一样。背景图片是常用来改变整个按钮的。
【解决方案2】:

//定义按钮的属性

声明未经检查的图像

- (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;
    }
}

【讨论】:

    【解决方案3】:

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

    或一个 x

    // 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];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多