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?
- In iOS, using CGPDFDocumentRef, why is my PDF showing big margins and not the whole page?
- objective-c ARC readonly properties and private setter implementation
- UISlider thumbTintColor doesn't change on iOS 7 (fine on iOS 6)
- Objective-C Debugging tips in Xcode4?
- swift ios mobile app login submit
- Writing handler for UIAlertAction
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).
- What does main.sync in global().async mean?
- Alternative ways to push view controllers with storyboard programmatically
- UIScrollView blocks run loop?
- Xcode 5: Multiple test targets in one scheme: “Simulator already in use”
- function with dataTask returning a value
- Smart-search for Parse Usernames in Swift not working
- JS calling a function in Swift
- Cocoapods: turning MagicalRecord logging off
- Xcode referencing old framework
- How do I alternate a UICollectiveViewCell's background color based on row?
- UIPopoverController dealloc getting called—ARC environment
- alamofire multipartformdata use urlrequest
- Programmatically turn on bluetooth in the iphone sdk?
- Z-index of iOS MapKit user location annotation
- iPhone – Auto resizing UIWebView content do not fit the frame