UISegmentedControl和View切换问题

UISegmentedControl and View switching problem


所以我编写了一个代码,其中我在 UISegmentedControl 中有 3 个选项,它应该向您显示 3 个视图之一(绿色带有 2 个文本字段,子视图蓝色带有 2 个按钮和子-使用 2 IUImageView 查看紫色)


起初一切看起来都很简单,但后来我意识到我的VC只有第一个View,而忽略了2和3。所以当我点击第二个和第三个分段控件时,我看到的是白屏。
很奇怪,但是当我更改 1 seg 控件以显示蓝色视图而不是绿色视图时,它还显示了白色屏幕。


我还是个初学者,所以我很难理解这里的问题在哪里(
也许有人可以帮助我。
谢谢!
hierarchy in storyboard

 import UIKit

class ThirdViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        greenView.isHidden = false. // in this part I've tried to make the second view visible instead of first. It did not work. 
        
        blueView.isHidden = true
        purpleView.isHidden = true
    }

    @IBOutlet weak var greenView: UIView!
    
    @IBOutlet weak var blueView: UIView!
   
    @IBOutlet weak var purpleView: UIView!
    
    @IBAction func didChangeSC(_ sender: UISegmentedControl) {
   
        switch  sender.selectedSegmentIndex {
        case 0 :
            greenView.isHidden = false
            
            blueView.isHidden = true
            purpleView.isHidden = true
            
        case 1 :
            blueView.isHidden = false
            
            greenView.isHidden = true
            purpleView.isHidden = true
        case 2 :
            purpleView.isHidden = false
            
            greenView.isHidden = true
            blueView.isHidden = true
            
            
        default:
            break
         
        }
    }
        
    
    
    

    /*
     // I've also tried to change the visibility but it didn't work.Here is how I did it.


     case 0 :
         greenView.alpha = 1
         
         blueView.alpha = 0
         purpleView.alpha = 0
         
     case 1 :
         blueView.alpha = 1
         
         greenView.alpha = 0
         purpleView.alpha = 0
     case 2 :
         purpleView.alpha = 1
         
         greenView.alpha = 0
         blueView.alpha = 0
     */

问题是您的 BlueViewPurpleViewGreenView 的子视图。由于它们是子视图,当您隐藏 GreenView 时,它们也会被隐藏。所以让所有三个视图成为兄弟姐妹(在同一级别)

现在您的视图层次结构低于

- View
  - GreenView
    - BlueView 
      - PurpleView 

改为

- View
  - GreenView
  - BlueView 
  - PurpleView