IBOutlet equals nil
I just tried connecting my UIWebView with UIViewController and when I started application I got error that mean UIWebView equals nil. What problem can be?
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
Instead, if you’re programmatically transitioning to this scene, you can instantiate the view controller using its “storyboard ID”:
Specify the storyboard ID for the view controller’s scene in the “Identity Inspector” in the right panel in Interface Builder:
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
AuthViewControllerusing 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:
So create a segue between the two scenes by control-dragging from the view controller icon above the original scene to the destination scene:
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.
Give that segue its own storyboard ID:
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
IBOutletin Interface Builder. Given that the “Auth Web View” label is showing up in the document outline, I presume you did hook up the
IBOutletsuccessfully, 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:
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).
- Swift: Terminating with uncaught exception of type NSException
- Can I get the modulus or exponent from a SecKeyRef object in Swift?
- CocoaPods with XCode 6.0.1 – How can I fix Pods Frameworks appearing in red?
- struggling with formatting dynamic row height in UITableView
- Display alert from a non-UI class
- Adding Navigation Bar programmatically iOS
- getter and setters not working objective c
- Is there a way to create an NSDecimal without using NSNumber and creating autoreleased objects?
- Can't put a single quote before the year in NSDateFormatter
- Load only five annotations to MKMapVIew
- clang: error: unsupported option '-static-libgcc' on Mac OSX Mavericks
- How to initialise a UIView Class with a xib file in Swift, iOS
- UIViewController – issue with custom dismiss transition
- iPhone icon size
- Objective-C: How to change the class of an object at runtime?