class ListItensController: UITableViewController, ViewControllerDelegate {
var refeicoes: [Meal] = []
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return refeicoes.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = refeicoes[indexPath.row].name
return cell
}
func add(_ refeicao: Meal){
refeicoes.append(refeicao)
tableView.reloadData()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "addMealSegue"){
if let viewController = segue.destination as? ViewController {
viewController.delegate = self
}
}
}
}
/*======================*/
protocol ViewControllerDelegate {
func add(_ refeicao: Meal)
}
class ViewController: UIViewController {
@IBOutlet weak var nameTf: UITextField?
@IBOutlet weak var noteTf: UITextField?
var delegate: ViewControllerDelegate?
@IBAction func add(_ sender: Any) {
if let name = nameTf?.text, let note = noteTf?.text {
if let noteL = Int(note) {
let meal: Meal = Meal(name: name, note: noteL)
delegate?.add(meal)
navigationController?.popViewController(animated: true)
}
}
}
}
O protocol não está funcionando, alguém pode me ajudar?