Search code examples
swiftxcodeparameter-passingnsbundlensexception

Swift Interesting NSExceptionError When Passing Data Forward


I have a really simple question here that I cannot manage to solve. This code works perfectly fine, leading me to the fifth view controller.

@objc func OnbtnPlusTouched()

{
    let uivc = storyboard!.instantiateViewController(withIdentifier: "FifthViewController")
    navigationController!.pushViewController(uivc, animated: true)

}

However, I want to pass the data locationName from this viewcontroller to the next one. Thus I used this code segment:

@objc func OnbtnPlusTouched()

{
    let vc = FifthViewController(nibName: "FifthViewController", bundle: nil)
    vc.locationName = locationName
    navigationController?.pushViewController(vc, animated: true)
}

But now I am getting this error as soon as I enter this function in my app:

libc++abi.dylib: terminating with uncaught exception of type

NSException and Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle

I would appreciate any help, thank you!


Solution

  • Try this instead

    @objc func OnbtnPlusTouched() {
        guard let uivc = storyboard!.instantiateViewController(withIdentifier: "FifthViewController") as? FifthViewController else {
             return 
        }
        uivc.locationName = locationName
        navigationController!.pushViewController(uivc, animated: true)
    }