使用复选框的最佳方式 - IOS swift [英] The best way to use checkbox - IOS swift

查看:737
本文介绍了使用复选框的最佳方式 - IOS swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将开发一个将使用大量复选框的项目。我找到了类似下面的解决方案,但我知道这不是正确的方法。

  @IBAction func btn_box(sender:UIButton){
if(btn_box.selected == true)
{
btn_box.setBackgroundImage(UIImage(named:box),forState:UIControlState.Normal)

btn_box。 selected = false;
}
其他
{
btn_box.setBackgroundImage(UIImage(名称:checkBox),forState:UIControlState.Normal)

btn_box.selected =真正;
}
}

所以,任何人都可以告诉我正确的方式我的项目中有20多个复选框?



我将在表格和设置目的中使用复选框。



谢谢。

解决方案

有很多


  • 设置按钮的默认图片:




  • 对于Progr ammatically:

      btn_box.setBackgroundImage(UIImage(名称:box),for:。normal)
    btn_box.setBackgroundImage(UIImage(名称:checkBox),用于:.selected)

    和在按钮操作中:

      @IBAction func btn_box(发件人:UIButton){
    sender.isSelected =!sender.isSelected
    }


    I am going to work on a project that will use a lot of checkboxes. I found a solution like below, but I know this not right way.

     @IBAction func btn_box(sender: UIButton) {
        if (btn_box.selected == true)
        {
            btn_box.setBackgroundImage(UIImage(named: "box"), forState: UIControlState.Normal)
    
                btn_box.selected = false;
        }
        else
        {
            btn_box.setBackgroundImage(UIImage(named: "checkBox"), forState: UIControlState.Normal)
    
            btn_box.selected = true;
        }
    }
    

    So, can anyone show me the right way of having more than 20 checkboxes in my project?

    I will use checkboxes in forms and for settings purpose.

    Thanks.

    解决方案

    There are lots of Checkbox control or you do it by this simple way:

    For Storyboard:

    1. Set your button's selected image:

    2. Set your button's default image:

    For Programmatically:

    btn_box.setBackgroundImage(UIImage(named: "box"), for: .normal)
    btn_box.setBackgroundImage(UIImage(named: "checkBox"), for: .selected)
    

    And in button action:

    @IBAction func btn_box(sender: UIButton) { 
        sender.isSelected = !sender.isSelected
    }
    

    这篇关于使用复选框的最佳方式 - IOS swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆