Reputation: 746
I've been getting an error when I'm trying to configuring my IBOutlets programmatically. Whenever I tried to setup my UI I've been getting this error:
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
But when I commented the code (the setupKategori()
function), my UI became transparent like this (you can see that only the Nav Bar is visible, where all of the items in the XIB file should've been visible):
Here's my code :
Setup UI function
func setupKategori() { // the function that throws the error whenever I call it in viewDidLoad()
kategoriLabel.font = .boldTitilliumWeb(ofSize: 18)
kategoriLabel.textColor = .init(hex: "#333333")
kategoriLabel.sizeToFit()
kategoriDetailLabel.font = .boldTitilliumWeb(ofSize: 18)
kategoriDetailLabel.textColor = .init(hex: "#222222")
kategoriDetailLabel.sizeToFit()
showKategoriButton.addTarget(self, action: #selector(showKategoriAction(_:)), for: .touchUpInside)
}
EDIT : Calling the View Controller
func routeToFilter() {
//Analytics.logEvent("pw_list_filter", parameters: nil)
let destinationVC = FilterPWVC()
let nav = UINavigationController(rootViewController: destinationVC)
present(nav, animated: true, completion: nil)
}
For just some information, I've already connected the IBOutlets from the XIB file. And I'm really new to iOS programming so I still have limited knowledge about it. If you need any more info feel free to ask and I will provide it to you. Any help would be appreciated. Thanks!
Upvotes: 1
Views: 79
Reputation: 9819
Since you are trying to instantiate your view controller using the init()
method (which in turn calls init(nibName:bundle:)
with nil parameters), then if you are expecting your view controller to be loaded from a xib file, your xib file's filename must be the same as your view controller's class name.
So, in your case, it should be: "FilterPWVC.xib", not "FilterPWView.xib".
OR, instead of renaming the xib and using init()
, you could also specify the nib name by using init(nibName:bundle:)
directly, like so:
// I'm assuming the xib is in the main bundle, so I've passed `nil`
// in for the bundle here
let destinationVC = FilterPWVC(nibName: "FilterPWView", bundle: nil)
Upvotes: 1