Olá, meu código parou de funcionar. Usando alguns Prints, vi que o delegate está nulo e o app parou de funcionar, ou seja, ao clicar em "enviar", o app nao volta para a tela anterior.
//
// ViewController.swift
// Primeiro-Projeto-Alura
//
// Created by Douglas B de souza on 16/07/17.
// Copyright © 2017 Douglas B de souza. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var nameField : UITextField?
@IBOutlet var happinessField : UITextField?
var delegate : AddAMealDelegate?
var selected = Array<Item>()
var items = [
Item(name: "Eggplant", calories: 10),
Item(name: "Brownie", calories: 10),
Item(name: "Zucchini", calories: 10),
Item(name: "Muffin", calories: 10),
Item(name: "Coconut oil", calories: 500),
Item(name: "Chocolate frosting", calories: 1000),
Item(name: "Chocolate chip", calories: 1000)
]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
if (cell.accessoryType == UITableViewCellAccessoryType.none) {
cell.accessoryType = UITableViewCellAccessoryType.checkmark
let item = items[indexPath.row]
selected.append(item)
}
else {
cell.accessoryType = UITableViewCellAccessoryType.none
let item = items[indexPath.row]
if let position = selected.index(of: item) {
selected.remove(at: position)
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: nil)
let row = indexPath.row
let item = items[row]
cell.textLabel!.text = item.name
return cell
}
@IBAction func add(){
if(nameField == nil || happinessField == nil){
return
}
let name:String = nameField!.text!
if let happiness = Int(happinessField!.text!){
let meal = Meal(name: name, happiness: happiness, items: selected)
print("eaten \(meal.name) with happiness \(String(meal.happiness))! with \(meal.items)!")
if (delegate == nil) {
return
}
delegate!.add(meal)
if let navigation = navigationController {
navigation.popViewController(animated: true)
}
}
}
}
Aqui, segue o meu AddAMealDelegate
import Foundation
protocol AddAMealDelegate {
func add(_ meal: Meal)
}