我的应用程序中有一个食物日记,可以跟踪您每天吃的食物,但我正在努力更新我的 UITableViewCell
中的卡路里标签。一按“保存到美食日记”,我的应用程序就崩溃了,错误如下:
"fatal error: unexpectedly found nil while unwrapping an Optional value"
这是我在 PopUpVC
中的代码(UIViewController
,用户可以看到它输入 FoodName
和 Calories
)
protocol AddRowDelegate {
func didAddRow(name : String, calories : String)
}
class PopupVC: UIViewController {
var delegate: AddRowDelegate?
var section: Int?
var caloriesLabel = " "
var tableData: [String] = [""]
var caloriesCell = caloriesForDiary()
@IBOutlet weak var foodTimeLabel: UILabel!
@IBOutlet weak var foodPopup2: UIView!
@IBOutlet weak var foodPopUp: UIView!
@IBOutlet weak var inputFood: UITextField!
@IBOutlet weak var inputCals: UITextField!
@IBAction func saveToDiary(_ sender: Any) {
dismiss(animated: true, completion: nil)
delegate?.didAddRow(name: inputFood.text!, calories: inputCals.text!)
}
然后我将食物和卡路里 UItextFields
的输入传递给我的 FoodDiaryVC
:
func didAddRow(name: String, calories: String) {
cals.calories.text = calories
print(name)
print(calories)
}
我添加了显示错误的图片和 ViewController
。我不确定为什么它会找到/返回零?我基本上只是试图将我的卡路里标签设置为等于输入(或全天卡路里的累积)
最佳答案
您的cals
或calories
对象是nil
,导致崩溃。您没有在发布的代码中包含此对象,但请确保在访问它之前实例化该对象。
关于ios - 无法使用委托(delegate)更新 UITableViewCell 中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43560953/