Swift PushViewController From A Different Storyboard

Marie.K

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

Charlie Fish

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.

edited at
0

Comments

0 comments
Login to comment

Related

PushViewController from another UIViewController - Swift

Segue from one storyboard to a different storyboard?

Swift - pushViewController from appDelegate, rootViewController.navigationController is nil

Pass data from one controller to another via segue and Navigation on different storyboard, Swift, iOS, Xcode

Swift send data to viewcontroller in different storyboard

Different portrait/landscape views in storyboard and Swift

pushViewController not work in swift 3

swift pushViewController memory

Xcode - Simulator shows views different from Storyboard

How to PushViewController From modalViewController

Swift - add custom color from Storyboard programmatically

Swift prevent storyboard segue from happening

(Swift) when does the Cell init from StoryBoard?

Instantiate Storyboard from local Swift Package

SWIFT - Storyboard instantiating from Objective-C

Swift / SKStoreProductViewController pushViewController results in (lldb)

Swift - How to use pushViewController in NSObject

xcode 6 swift cant add existing objects to different device storyboard

navigationController pushViewController from UIPageViewController not work

Call pushViewController from external class?

Input from UITextField connected from storyboard to Swift view controller file

How can I use the same ViewController from a storyboard with different sizes?

App crashing when instantiating ViewController from different storyboard

Views dont render on ViewController presented programmatically from a different storyboard

Navigating from one ViewController to a new ViewController with a different Storyboard

self.navigationController?.pushViewController not Working Swift

navigationController!.pushViewController vs. presentViewController in iOS Swift

swift - Initialize view controller from storyboard by overriding init

how to call xib file from storyboard on button click in swift