使用 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 (将#修改为@)