0

I have a viewController with two embedded viewControllers through containers. I then created outlets from the parent viewController's containers to the parent class. I want to either hide or show the containers depending on certain conditionals.

But if I simply write:

  @IBOutlet var twoArmsContainer: UIView! {
        didSet {
            print("SETTING TWO ARM")
        }
    }

 override func viewDidLoad() {
        super.viewDidLoad()
        twoArmsContainer.isHidden = true //container is nil
}

Then it crashes with twoArmsContainer being nil after the print in didSet has been triggered. How is it possible that the outlet is set, but then becomes nil? I have tried hiding it inside didSet and that works fine:

  @IBOutlet var twoArmsContainer: UIView! {
        didSet {
            print("SETTING TWO ARM")
            twoArmsContainer.isHidden = true //WORKS
        }
    }

What else can I say? The class I'm working in inherits from another class so there is a super.viewDidLoad. Not sure if that is relevant. I tried putting the outlets in the super class but with the same results. I also tried removing and readding the outlets again. Have never experienced this problem before. Let me know if I should show more code; perhaps the entire class. Not really sure what's relevant as I'm clueless of where to start.

Joakim Sjöstedt
  • 824
  • 2
  • 9
  • 18
  • I think the container is load after the viewDidLoad. You should give a try by placing your code in the viewDidAppear, just to check – CZ54 Jan 26 '21 at 09:35
  • 1
    @CZ54 Ah, interesting, I'll check! – Joakim Sjöstedt Jan 26 '21 at 09:42
  • How are you creating the view controller instance? Is it being created from the storyboard? – Paulw11 Jan 26 '21 at 10:42
  • @JoakimSjöstedt - double check that you set the `IBOutlet` correctly. I just gave it a quick try and cannot reproduce the issue. – DonMag Jan 26 '21 at 12:19
  • 1
    All the views are always ready for being used right after viewDidLoad is called! What @CZ54 says is actually about the actual embedding process of the child view controller into to that container views which happens a bit later than viewDidLoad is called. – arturdev Jan 26 '21 at 18:30
  • @JoakimSjöstedt You problem interested me. Could you provide a small example project where I can reproduce the issue? – arturdev Jan 26 '21 at 18:31

1 Answers1

0

Ok, so I found out what was wrong; an issue impossible to detect without access to the code, so in retrospect I should have posted both the parent class and container class. Sorry about that.

Anyway the issue was that the container viewController inherited from the parent viewController. This enabled me to share code in the two container viewControllers.

So the structure was basically this:

class WizardChooseArmViewController: WizardViewController {
...

This is the parent viewController which inherits from a base viewController. Then the container viewControllers looked like this:

final class WizardTwoArmsViewController: WizardChooseArmViewController {
...

Apparently it's a bad idea for containers to inherit from its parent, so I refactored and changed it to:

final class WizardTwoArmsViewController: WizardViewController {
... 

Not quite sure why its not possible for containers to inherit from its parent. Would be great if someone could brief me.

Joakim Sjöstedt
  • 824
  • 2
  • 9
  • 18