I'm trying to show one of my ViewControllers
of a different storyboard programmatically. But for some reasons the properties of the ViewController
I'm trying to show are nil.
That's my error: Unexpectedly found nil while unwrapping an Optional value
I'm getting the error in my
SecondViewController
. It is showing but my app crashes due to the properties being nil.
That's my code:
let secondStoryBoard = UIStoryboard(name: "SecondStoryBoard", bundle: nil)
let secondViewController = secondStoryBoard.instantiateViewController(withIdentifier: "secondVC") as! SecondVC
self.navigationController?.pushViewController(secondViewController, animated: true)
That's the code producing the error:
class SecondVC: ViewController
{
@IBOutlet weak var mapView: MKMapView!
mapView.isHidden = true // ERROR!! Unexpectedly found nil while unwrapping an Optional value
}
SOLVED: just had to reconnect my
@IBOutlet
connections
This means that the following line is either nil
or not of type SecondVC
.
secondStoryBoard.instantiateViewController(withIdentifier: "secondVC")
I would verify in your storyboard that you have an identifier secondVC
and that view controller is of type SecondVC
.
Edit:
Based on the new code the OP posted in the question, changing the SecondVC to the following should work.
class SecondVC: ViewController
{
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
mapView.isHidden = true
}
}
Basically this will wait until the view has loaded before accessing the mapView
. For the original code, you were trying to access mapView
before it has actually been loaded.
Edit 2:
It looks like mapView
outlet is not properly linked to the storyboard.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments