0

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!

Enea Dume
  • 3,014
  • 3
  • 21
  • 36
Ege Akkaya
  • 176
  • 1
  • 13
  • 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 Aug 25 '18 at 10:11
  • @JoakimDanielson that's because this is the only way I know to pass a data forward in this situation :-( – Ege Akkaya Aug 25 '18 at 10:16
  • Did you also move the view and controller from the storyboard to a XIB? – Willeke Aug 25 '18 at 10:17
  • @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. – Ege Akkaya Aug 25 '18 at 10:19
  • You can't load a view controller from a nib if the nib doesn't exist. – Willeke Aug 25 '18 at 10:27

1 Answers1

0

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)
}
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • You shouldn't force unwrap `storyboard` here. With a optional chaining you can check for both cases in one statement – heyfrank Aug 25 '18 at 10:26