• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iOS/swift单选框和复选框

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
/**
 复选框
 */
import UIKit
 
class LYBmutipleSelectView: UIView {
 
    var selectindexs:[Int]=[]//选中的
 
    //标题数组
    var titleArr:[String]=[""]{
        didSet{
            for i in  0..<titleArr.count{
                //组装按钮和label
                let   singleselectview:UIView=UIView.init(frame: CGRect.init(x: i*100, y: 100, width: 100, height: 50))
                
                let rightLbel:UILabel=UILabel.init(frame: CGRect.init(x: 50, y: 0, width: 50, height: 50))
                rightLbel.text=titleArr[i]
                  singleselectview.addSubview(rightLbel)
                
                let leftBtn:UIButton=UIButton.init(frame: CGRect.init(x: 10, y: 10, width: 30, height: 30))
                leftBtn.tag=130+i;
                leftBtn.setImage(UIImage.init(named: "fuxuankuangUnselect"), for: UIControl.State.normal)
                leftBtn.addTarget(self, action: #selector(leftBtnClcik), for: UIControl.Event.touchUpInside)
                singleselectview.addSubview(leftBtn)
              
                addSubview(singleselectview)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initViews(){
        let sureBtn:UIButton=UIButton.init(frame: CGRect.init(x: 200, y: 10, width: 100, height: 50))
        sureBtn.setTitle("确认", for: UIControl.State.normal)
        sureBtn.setTitleColor(UIColor.black, for: UIControl.State.normal)
        sureBtn.addTarget(self, action: #selector(sureBtnClcik), for: UIControl.Event.touchUpInside)
        addSubview(sureBtn)
    }
    
    //确认按钮,根据选中的按钮索引做相应的操作
    @objc func sureBtnClcik(){
        print("\(selectindexs)")
    }
    
    //点击按钮选中或取消
    @objc func leftBtnClcik(sender:UIButton){
        sender.isSelected = !sender.isSelected
        let btnTag:Int=sender.tag-130
        if sender.isSelected{//选中
            selectindexs.append(btnTag)//吧按钮的索引存储起来
        }else {
            //删除数组中的元素,采用过滤的方法,swift中没有现成f的方法
            let fiflter:[Int]=selectindexs.filter {
                $0 != btnTag
            }
            selectindexs = fiflter
        }
        
        sender.setImage(UIImage.init(named: "fuxuankuangUnselect"), for: UIControl.State.selected)
         sender.setImage(UIImage.init(named: "fuxuankuangselect"), for: UIControl.State.selected)
    }
    
    
}
/**
  单选框
  */
 
import UIKit
 
class LYBSingleselectview: UIView {
 
    var selectindex:Int=0//选中的
    var lastbtn:UIButton=UIButton.init()//保存上一个按钮
    
    //标题数组
    var titleArr:[String]=[""]{
        didSet{
            for i in  0..<titleArr.count{
                //组装按钮和label
                let   singleselectview:UIView=UIView.init(frame: CGRect.init(x: i*100, y: 100, width: 100, height: 50))
                
                let rightLbel:UILabel=UILabel.init(frame: CGRect.init(x: 50, y: 0, width: 50, height: 50))
                rightLbel.text=titleArr[i]
                singleselectview.addSubview(rightLbel)
                
                let leftBtn:UIButton=UIButton.init(frame: CGRect.init(x: 10, y: 10, width: 30, height: 30))
              leftBtn.tag=130+i
                leftBtn.setImage(UIImage.init(named: "fuxuankuangUnselect"), for: UIControl.State.normal)
                leftBtn.addTarget(self, action: #selector(leftBtnClcik), for: UIControl.Event.touchUpInside)
                singleselectview.addSubview(leftBtn)
                
                addSubview(singleselectview)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initViews(){
        let sureBtn:UIButton=UIButton.init(frame: CGRect.init(x: 200, y: 10, width: 100, height: 50))
        sureBtn.setTitle("确认", for: UIControl.State.normal)
        sureBtn.setTitleColor(UIColor.black, for: UIControl.State.normal)
        sureBtn.addTarget(self, action: #selector(sureBtnClcik), for: UIControl.Event.touchUpInside)
        addSubview(sureBtn)
    }
    
    //确认按钮,根据选中的按钮索引做相应的操作
    @objc func sureBtnClcik(){
        print("\(selectindex)")
    }
    
    //点击按钮选中或取消
    @objc func leftBtnClcik(sender:UIButton){
        let btnTag:Int=sender.tag-130
       sender.isSelected=true
        lastbtn.isSelected=false
        lastbtn.setImage(UIImage.init(named: "fuxuankuangUnselect"), for: UIControl.State.selected)
        sender.setImage(UIImage.init(named: "fuxuankuangselect"), for: UIControl.State.selected)
         lastbtn=sender
        selectindex = btnTag
    }
    
    
}


原文链接:https://blog.csdn.net/u011146511/java/article/details/86578730


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode234.回文链表|PalindromeLinkedList发布时间:2022-07-13
下一篇:
编译命令行终端swift发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap