IBOutlet is nil, but it is connected in storyboard, Swift
The storyboard wasn't recognizing any further UI things I added to it. At run time all the references were nil. So I cleared my derived data folder and then those connections worked again.
IBOutlet is nil
To see where the outlet is being set to nil, try this:
@IBOutlet weak var labelView: UIView? {
didSet {
print("labelView: \(labelView)")
}
}
You should see it set to an initial value when the view is loaded. If it then gets set to nil
, put a breakpoint on the print
and your should be able to see from the backtrace where it's happening.
Swift/iOS: IBOutlet nil after loading view controller
The problem is the reference to InfoViewController()
, which instantiates the view controller independent of any storyboard scene. You want to use instantiateViewController
:
let infoViewController = storyboard?.instantiateViewController(withIdentifier: "Info") as! InfoViewController
infoViewController.modalPresentationStyle = .overCurrentContext
present(infoViewController, animated: true) {
infoViewController.displayInfo()
}
A couple of notes:
This assumes that (a) you've given the scene in the storyboard a "storyboard id"; (b) you've set the base class for that scene to
InfoViewController
.Note, I called
displayInfo
in the completion handler ofpresent
because you probably don't want that called until the scene has been presented and the outlets have been hooked up.
Alternatively, you can update non-outlet properties of the InfoViewController
immediately after instantiating it and then have its viewDidLoad
take those properties and update the outlets, e.g.:
class InfoViewController: UIViewController {
var info: String!
@IBOutlet weak var infoLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
infoLabel.attributedText = NSAttributedString(string: info)
}
}
Note, I changed the @IBOutlet
name to be infoLabel
and added the String
property called info
. That tends to be the convention, that outlets bear some suffix indicating the type of control, and model objects, like the String
property, are without the suffix. (You'll just want to make sure you remove that old outlet in the connections inspector in IB so that you don't have problems with these property name changes.)
Anyway, you can then do:
let infoViewController = storyboard?.instantiateViewController(withIdentifier: "Info") as! InfoViewController
infoViewController.info = "abc"
infoViewController.modalPresentationStyle = .overCurrentContext
present(infoViewController, animated: true, completion: nil)
The key point is don't try to update outlets of the scene immediately after instantiating it, but make sure that this is deferred until after viewDidLoad
was called.
IBOutlet elements come out nil when I try to set them in viewDidLoad
I've had the same problem under iOS7 on very slow devices.
Just wait for your views to be rendered in viewDidLayoutSubviews then do your changes.
Don't forget to do so just once though for viewDidLayoutSubviews is being called a lot.
var first = false
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if self.first {
//Update your views
self.first = false
}
}
You could also use view.layoutSubviews() but I recommend you not to ;)
EDIT
I did not see the problem at first sight but you are creating your view controllers from scratch! You must instantiate them using a storyboard or your IBOutlets won't be set ;)
var vc1 = storyboard?.instantiateViewControllerWithIdentifier("YourControllerStoryboardId") as! CarouselViewController
Hope this'll help!
IBOutlet nil - button
It’s not a “bug”. Your code is what’s at fault. It has nothing to do with pushing vs presenting. It’s that this line is wrong:
let v = RegisterController()
That creates a barebones view controller with no outlets hooked up. The outlets are hooked up in the storyboard instance of this class. Create the view controller from the storyboard as you did in the first code, and all will be well.
let v = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "signup") as! RegisterController
Xib's IBOutlets returning nil when using Xib in storyboard
This is happening Because the xib was never loaded
So what you can do here is, First add a simple UIView
in the viewController rather than TimeRangeView
@IBOutlet var timeRangeContainerView: UIView!
Now in viewDidLoad
add Fetch the xib
by using
let timeRangeView = Bundle.main.loadNibNamed("TimeRangeView", owner: self, options: nil)?[0] as? TimeRangeView
And add this as a subview of timeRangeContainerView
override func viewDidLoad() {
super.viewDidLoad()
let timeRangeView = Bundle.main.loadNibNamed("TimeRangeView", owner: self, options: nil)?[0] as? TimeRangeView
timeRangeView.frame = CGRect(origin: CGPoint.zero, size: self.timeRangeContainerView.frame.size)
self.timeRangeContainerView.insertSubview(timeRangeView, at: 0)
// Add constraints
timeRangeView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 10).isActive = true
timeRangeView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true
timeRangeView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
timeRangeView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
timeRangeView.translatesAutoresizingMaskIntoConstraints = false
//Add the rest of your code here
}
The constraints are optional
Related Topics
Facebook Graphrequest for Swift 5 and Facebook Sdk 5
Make Tabview Background Transparent
Difference Between @Propertydelegate and @Propertywrapper
Convert from Nsdictionary to [String:Any]
Nscollectionview Selection Handling in Swift
How to Test Whether an Array's Type Is Optional
Swift: Using Member Constant as Default Value for Function Parameter
Why Do I Get Source Kit Service Terminated Error
Enum Named 'Type' in Nested Class Fails
Swift Switch Case Compiler Error
How to Mutate an Array in a Dictionary
+' Is Deprecated: Mixed-Type Addition Is Deprecated in Swift 3.1
Localize iOS 14 Dynamic Widget Configuration
Sbdata Is Wrong When Sbvalue Comes from a Swift Dictionary
What Does an Underscore "_" Mean in Swift
Extend All Number Types in Swift
Swift Playground with Debugger Support
How to Get The Push Notifications Displayed in The Notification Center