简体   繁体   中英

Swift: I got “ Unexpectedly found nil ” in “tableView.delegate=self”

I connect a segue between a button and a view controller as shown in the screenshoot. 我在按钮和视图控制器之间连接一个segue。

Everything works until I choose the class as "ViewController". 在此输入图像描述

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" 在此输入图像描述

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

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.

在此输入图像描述

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

在此输入图像描述

See pic.

在此输入图像描述

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.

检查所有插座是否正在使用Connections检查器,还检查您是否继承UIViewController,UITableViewDelegate和UITableViewDataSource并且所有内容都是属性连接的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM