0
votes

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!

1
Why did you need to change how the view controller is created just because you want to set a property? Why not do a typecast instead?Joakim Danielson
@JoakimDanielson that's because this is the only way I know to pass a data forward in this situation :-(Bobby dickson
Did you also move the view and controller from the storyboard to a XIB?Willeke
@Willeke I didn't touch anything in the storyboard, I only added titles to my storyboards some time ago but that's it. I don't understand how the above code can work but the below one can't.Bobby dickson
You can't load a view controller from a nib if the nib doesn't exist.Willeke

1 Answers

0
votes

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)
}