0

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

Marie.K
  • 37
  • 8

1 Answers1

1

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.

Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
  • Thank you, I don't think I explained it enough. I edited my question – Marie.K Dec 28 '18 at 19:56
  • @Marie.K I don't understand your edit. `It is showing but my app crashes due to the properties being nil.`. Which properties are nil? Can you show the line it's actually crashing on? – Charlie Fish Dec 28 '18 at 19:57
  • Sorry, I edited it again – Marie.K Dec 28 '18 at 19:59
  • @Marie.K Just edited my answer. – Charlie Fish Dec 28 '18 at 20:02
  • @CharlieFish how OP can run his code in global scope of class? I think problem is not here – Robert Dresler Dec 28 '18 at 20:03
  • @RobertDresler If you are initializing `mapView` in the storyboard, I don't think there is anyway to run it in the global scope of the class. – Charlie Fish Dec 28 '18 at 20:03
  • Sorry again. I am doing that already. And if I load this view using a segue it works. But like this from a different storyboard, it doesn't. thank you for being so patient with me – Marie.K Dec 28 '18 at 20:04
  • @Marie.K Have you made sure `mapView` is linked to the outlet in your storyboard? – Charlie Fish Dec 28 '18 at 20:04
  • Yes, everything works fine I only get the error when trying to show it programmatically from a different storyboard. – Marie.K Dec 28 '18 at 20:05
  • @CharlieFish yes, but you didn't solve the problem. I'm telling you, that is code can't be run in global scope so he probably calls it somewhere else like you do in `viewDidLoad`. – Robert Dresler Dec 28 '18 at 20:05
  • @RobertDresler Got it. I was just relying on the OPs original code in the question being what they were actually using. – Charlie Fish Dec 28 '18 at 20:07
  • @Marie.K But are you able to see this view controller at all? Like does this view controller ever work? And if so, how are you loading it when it works? – Charlie Fish Dec 28 '18 at 20:07
  • 1
    @CharlieFish I think this is just a very common problem with bad connection of `IBOutlet` – Robert Dresler Dec 28 '18 at 20:08
  • @RobertDresler Yeah that is what it looks like to me too. I commented that above. Really don't see how it can be anything other than that. – Charlie Fish Dec 28 '18 at 20:09
  • 1
    Okay, guys thank you I just disconnected and reconnected my IBOutlet. It works now – Marie.K Dec 28 '18 at 20:10