0

What I'm trying to do:

I am trying to press a + button on the top right side of the navigation bar, push the app to a new view (using navigationController.pushViewController), get some user input text from the new view, then return back to the original view.

enter image description here

Problem:

Below code returns nil, which causes the navigationController.pushViewController to crash the app. So again, I do

let newPostView = self.storyboard?.instantiateViewController(withIdentifier: "PostNewActivityViewController") as? PostNewActivityViewController

then the line below crashes the app, because the newPostView is nil.

navigationController?.pushViewController(newPostView!, animated: true)

And all the error message I get from this exception is EXC_BAD_INSTRUCTION and the console output is only telling me that it unexpectedly found nil while unwrapping an Optional value. I don't understand why newPostView is nil.

Also, I am wondering if this is the best practice to obtain user input from another view controller.

Any help or feedback will be much appreciated.

whgnsdlr7
  • 77
  • 2
  • 9
  • 2
    check if the you actually set the controller `storyboard id`, or if the controller is associated to `PostNewActivityViewController` class – Lamour Apr 17 '17 at 01:35
  • 1
    Use the UIStoryboard instead. – Mannopson Apr 17 '17 at 01:38
  • 3
    @matt, are you mad, man? Figure it out for yourself?!?! That's what the internet is for! – Duncan C Apr 17 '17 at 01:48
  • @Lamar Just did a p self.storyboard and it came out as nil. Is this because I don't have a storyboard scene for it? I never used storyboard before, as I learned everything to be done in programmatically thus far. – whgnsdlr7 Apr 17 '17 at 06:10
  • @matt I have debugged this for some time and I am fairly new to Swift programming. I couldn't figure it out even after hours of searching, so I came here to help. – whgnsdlr7 Apr 17 '17 at 06:15

1 Answers1

1

In your comment, you mention that p self.storyboard (in the debugger) is producing (printing) nil.

So I suggest you get a reference to the Storyboard in the following way instead:

let storyboard = UIStoryboard(name: "YOUR STORYBOARD'S NAME (e.g. Main)", bundle: Bundle.main)

And then proceed to instantiate and present your UIViewController in the same way, like this:

let viewController = storyboard.instantiateViewController(withIdentifier: "PostNewActivityViewController")
present(viewController, animated: true, completion: nil)

I hope this helps. Let me know if you need any further clarification.

Loic Verrall
  • 985
  • 15
  • 25
  • Thanks for your reply. So far I didn't even touch the storyboard and the there are only two default ones (Main.storyboard and LaunchScreen.storyboard). I did try your suggestion, and it crashed in AppDelegate with the following reason: 'Storyboard () doesn't contain a view controller with identifier 'PostNewActivityViewController'' – whgnsdlr7 Apr 17 '17 at 21:07
  • Okay, and the Storyboard containing the PostNewActivityViewController is definitely the same one (Main)? Also, check that you have set the PostNewActivityViewController's Storyboard ID to "PostNewActivityViewController" – Loic Verrall Apr 18 '17 at 05:59
  • 1
    I ended up solving the problem other way around, as this solution requires me to use the storyboard which I did not use at all. But thank you though! – whgnsdlr7 Apr 20 '17 at 06:20