0

I am having a weird issue where my app crashes when I am trying to push a new view controller. I have set up a swipe gesture and want to segue to another view controller when a swipe is detected. When I run these 2 lines of code ...

let viewController:ViewController = ViewController()
self.navigationController?.pushViewController(viewController, animated: true)

The app crashes not specifically on either of those lines of code but rather in my ViewController class when in my viewDidLoad method I run this piece of code...

imageView.layer.masksToBounds = true

If I comment that out it crashes when I set the auto-correction type of my textField. What am I doing wrong?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
coder
  • 381
  • 2
  • 22

4 Answers4

1

First place I look when the view immediately crashes is in the Outlets for that ViewController in InterfaceBuilder. I look for anything that shows up with an exclamation mark. That usually means I renamed an outlet or broke a connection somehow. Delete anything broken by pressing the little x by the item that's messed up. I'll attach a photo so you can see.

enter image description here

Dave Levy
  • 1,036
  • 13
  • 20
0

It seems you're loading a ViewController that exists in storyboard with

let viewController:ViewController = ViewController()

which will result in nil outlets , so you have to use

let viewController = storyboard.instantiateViewController(withIdentifier: "VCID") as! ViewController

and give that vc a storyboard identifier like VCID

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • I followed your suggestion (changed that line of code and gave the storyboard a corresponding id) but now the app crashes on the line of code in your answer, when I try to instantiate the viewcotnroller. Any idea why? – coder Nov 26 '18 at 15:52
  • in the log it just says lldb, Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value – coder Nov 26 '18 at 16:05
  • make sure the hardcoded id is the same , also default storyboard is Main , is your vc inside it ?? – Shehata Gamal Nov 26 '18 at 16:16
  • I have made sure the hardcoded id is the same, and my vc is inside of Main.storyboard – coder Nov 26 '18 at 16:51
  • can you share a github demo with the problem but fastly – Shehata Gamal Nov 26 '18 at 16:52
0

I think I have solved the issue but it has a weird side effect. Instead of using the line of code in @Sh_Khan's answer, I used ...

let viewController = nav?.storyboard!.instantiateViewController(withIdentifier: "mainVC") as! ViewController

The variable nav is equal to the navigation controller of the current view-controller. This seems to work without any hiccups but for some reason the back button does not disappear from the navigation controller after the segue is preformed. Does anybody know a solution to this, if so leave a comment and I will update my answer.

EDIT: Another issue is that it wipes everything changed on that ViewController by the user clear. Is there another way to instantiate a ViewController without clearing it?

coder
  • 381
  • 2
  • 22
  • It might be wiping everything because instantiating a VC like this creates a whole new instance of that VC, which will not be the same one that may already exist. – Chris Nov 26 '18 at 20:15
-1

Follow what @Sh_Khan has said and in addition to that make sure that the view that you are making the push segue from is embedded in a Navigation controller.

Cedan Misquith
  • 1,134
  • 9
  • 20