微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 UIPIckerView didselectrow 使用 isHidden 更改子视图

如何解决使用 UIPIckerView didselectrow 使用 isHidden 更改子视图

在模态视图中,我使用 UIPickerView 动态隐藏/显示子视图,目的是根据用户选择(通过选择器视图)创建表单字段。选择器视图的案例是一个枚举(此处显示为字符串),我应用 switch 语句来隐藏/显示排列在堆栈视图中的子视图。总共有三个抽象状态:0 = 没有添加 subview ,1 = 添加 subviewX,2 = 添加 subviewY(switch 语句依赖于 .isHidden 而不是 .removeFromSuperView)。

换种说法:

pickerview index 0 -> subviewX.isHidden = true,subviewY.isHidden = true (default view)
pickerview index 1 -> subviewX.isHidden = false,subviewY.isHidden = true
pickerview index 2 -> subviewY.isHidden = false,subviewX.isHidden = true

问题:

如果用户选择了pickerview中的第二项(pickerview index = 1),则用户更改为pickerview index = 2,subviewY被禁用且无法与(UIButtons数组)交互。

我正在研究、试验和“出错”的内容包括:firstresponder、layoutsubviews、setNeedsLayout、removeFromSuperview。

感谢任何帮助。

代码如下:

        foodPickerSelectedindex = row
        let value = foodCategoryArray[row]
        switch value {
        case "Solid":
      
            allergyLabel.isHidden = false
            allergySwitch.isHidden = false
            noAllergyLabel.isHidden = false
            foodTagsstack.isHidden = false

            leftSideNursingLabel.isHidden = true
            rightSideNursingLabel.isHidden = true
            leftOrRightNursing.isHidden = true
            
            
            NSLayoutConstraint.activate([
                foodTagsstack.topAnchor.constraint(equalTo: foodCatPicker.bottomAnchor,constant: 10),foodTagsstack.leadingAnchor.constraint(equalTo: modalScreen.leadingAnchor,constant: 20),foodTagsstack.trailingAnchor.constraint(equalTo: modalScreen.trailingAnchor,constant: -20),allergyStacks.topAnchor.constraint(equalTo: foodTagsstack.bottomAnchor,constant: 30),allergyStacks.leadingAnchor.constraint(equalTo: modalScreen.leadingAnchor,allergyStacks.trailingAnchor.constraint(equalTo: modalScreen.trailingAnchor,allergyStacks.bottomAnchor.constraint(equalTo: entryStacks.topAnchor,constant: -10),])
        case "Nursing":

            leftSideNursingLabel.isHidden = false
            rightSideNursingLabel.isHidden = false
            leftOrRightNursing.isHidden = false
            
            allergyLabel.isHidden = true
            allergySwitch.isHidden = true
            noAllergyLabel.isHidden = true
            foodTagsstack.isHidden = true
    
            NSLayoutConstraint.activate([
            
                nursingStack.topAnchor.constraint(equalTo: foodCatPicker.bottomAnchor,nursingStack.leadingAnchor.constraint(equalTo: modalScreen.leadingAnchor,nursingStack.trailingAnchor.constraint(equalTo: modalScreen.trailingAnchor,nursingStack.bottomAnchor.constraint(equalTo: entryStacks.topAnchor,])

        default:

            foodTagsstack.isHidden = true
            allergyLabel.isHidden = true
            allergySwitch.isHidden = true
            noAllergyLabel.isHidden = true
            
            leftSideNursingLabel.isHidden = true
            rightSideNursingLabel.isHidden = true
            leftOrRightNursing.isHidden = true
        }
        
        //modalScreen.updateConstraints()
    }
}```

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。