Home>

I want to make some screen transitions with UIContainerView, but when I build it, it crashes with Unexpectedly found nil while implicitly unwrapping an Optional value.

class MainViewController: UIViewController {

    @IBOutlet weak var ViewC1: UIView!
        @IBOutlet weak var ViewC2: UIView!
        @IBOutlet weak var ViewC3: UIView!
    @IBOutlet weak var ViewC4: UIView!

    @IBAction func View1 (_ sender: UIButton) {
        ViewC1.isHidden = false
        ViewC2.isHidden = true
        ViewC3.isHidden = true
        ViewC4.isHidden = true
    }

    @IBAction func View2 (_ sender: UIButton) {
        ViewC1.isHidden = true
        ViewC2.isHidden = false
        ViewC3.isHidden = true
        ViewC4.isHidden = true
    }

    @IBAction func View3 (_ sender: UIButton) {
        ViewC1.isHidden = true
        ViewC2.isHidden = true
        ViewC3.isHidden = false
        ViewC4.isHidden = true
    }

    @IBAction func View4 (_ sender: UIButton) {
        ViewC1.isHidden = true
        ViewC2.isHidden = true
        ViewC3.isHidden = true
        ViewC4.isHidden = false
    }

    override func viewDidLoad () {
        super.viewDidLoad ()
        ViewC1.isHidden = false
        ViewC2.isHidden = true
        ViewC3.isHidden = true
        ViewC4.isHidden = true
        // Do any additional setup after loading the view.
    }

Even if @IBoutlet and @IBAction are linked again, this will happen.

Please tell me how to deal with it. .. Thank you.

  • Answer # 1

    As per the error message. ViewC1 is nil.
    Since there is no problem on the Swift side, the storyboard association is incorrect.

    A common case is that the tying is repeated many times and a lot of garbage is tied.
    All you have to do is delete unnecessary links.