IBOutlet equals nil

Solutions Collect From Internet About “IBOutlet equals nil”

You’ll see this sort of behavior if you just referenced AuthViewController(), but didn’t reference the storyboard at all. That’s because when you simply create an instance of a AuthViewController, it has no way of knowing what scene in the storyboard that class is associated with. Thus, all of the IBOutlet references will be nil.

Instead, if you’re programmatically transitioning to this scene, you can instantiate the view controller using its “storyboard ID”:

  1. Specify the storyboard ID for the view controller’s scene in the “Identity Inspector” in the right panel in Interface Builder:

    enter image description here

  2. Once you do that, rather than doing something like the following, which will not work:

    let controller = AuthViewController()
    // now do something with `controller`, e.g.
    presentViewController(controller, animated: true, completion: nil)

    You can now properly instantiate this AuthViewController using its storyboard ID:

    let controller = storyboard?.instantiateViewControllerWithIdentifier("AuthScene")
    presentViewController(controller!, animated: true, completion: nil)

Alternatively, if you want to transition between two scenes, I prefer to use the storyboard segues:

  1. So create a segue between the two scenes by control-dragging from the view controller icon above the original scene to the destination scene:

    enter image description here

    That creates a segue which isn’t linked to any buttons or anything like that. It’s just a segue directly between the two scenes that won’t be invoked until you do so programmatically.

  2. Give that segue its own storyboard ID:

    enter image description here

  3. Now you can perform that segue programmatically:

    performSegueWithIdentifier("TransitionToAuthScene", sender: self)

The advantage of this latter approach is that (a) the storyboard now visually represents the flow of your application; and (b) you can use unwind segues or the like.

A couple of final thoughts:

  • If you didn’t programmatically transition to this scene, the other possibility is that you didn’t hook up the IBOutlet in Interface Builder. Given that the “Auth Web View” label is showing up in the document outline, I presume you did hook up the IBOutlet successfully, but it is the other thing to check. You can confirm this by selecting the web view and then looking at the “Connections Inspector” panel on the right in Interface Builder:

    enter image description here

  • Later in comments you made reference to performing this transition in viewDidLoad. This is invariably the wrong place to be transitioning to a new scene from your storyboard, because you’re frequently going to be in the middle of transitioning to the scene in question, and iOS is going to get confused.

    It’s better to defer this sort of code until viewDidAppear (or skipping the “source” view controller and transitioning directly to this “auth” scene).