3

I connect a segue between a button and a view controller as shown in the screenshoot. I connect a segue between a button and a view controller.

Everything works until I choose the class as "ViewController". enter image description here

Every time I click the "Add" button, the code crashes with following error:"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" enter image description here

I'm really confused now. Can anyone help me this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

3 Answers3

6

You are getting crash bcoz table view is not connected as IBOutlet.

You need to connect table view IBOutlet in to view controller. You can do it by assistant editor top right corner (a double circle button intersecting each other) and drag and drop by pressing right click from table view to the view controller.

Once IBOutet is connected, in view controller there will be a dark gray color circle in before a property indicates that it will be connected successfully.

enter image description here

If circle is not filled then it means that property is not connected as IBOutlet.

enter image description here

See pic.

enter image description here

Mahendra
  • 8,448
  • 3
  • 33
  • 56
1

You are getting crash because you did not set outlet of tableview with storyboard. Please add outlet first and then set delegate.

Make sure while you get such crashes you must check that your control is not nil.

Anita Nagori
  • 707
  • 1
  • 7
  • 21
1

Check that all your outlets are working on the Connections inspector, also check that you are inheriting from the classes UIViewController, UITableViewDelegate and UITableViewDataSource and everything is property connected.