Não consigo localizar o porque da mensagem de erro abaixo: type 'ViewController' does not conform to protocol 'AddAnItemDelegate'
Segue o código do ViewController.swift:
import UIKit
protocol AddAMealDelegate {
func add(meal: Meal)
}
class ViewController: UIViewController,
UITableViewDataSource,
UITableViewDelegate, AddAnItemDelegate {
var items = [ Item(name: "Eggplant Brownie", calories: 10),
Item(name: "Zucchini Muffin", calories: 10),
Item(name: "Cookie", calories: 10),
Item(name: "Coconut oil", calories: 500),
Item(name: "Chocolate frosting", calories: 1000)
]
@IBOutlet var nameField: UITextField!
@IBOutlet var happinessField: UITextField!
var delegate:AddAMealDelegate?
var selected = Array<Item>()
@IBOutlet var tableView: UITableView?
func addnew(item: Item) {
items.append(item)
if let table = tableView {
table.reloadData()
} else {
Alert(controller: self).show()
}
}
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let row = indexPath.row
let item = items[ row ]
var cell = UITableViewCell(style: UITableViewCellStyle.Default,
reuseIdentifier: nil)
cell.textLabel!.text = item.name
return cell
}
func tableView(tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath){
if (cell.accessoryType == UITableViewCellAccessoryType.None) {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
selected.append(items[indexPath.row])
} else {
cell.accessoryType = UITableViewCellAccessoryType.None
if let position = find(selected, items[indexPath.row]) {
selected.removeAtIndex(position)
} else {
Alert(controller: self).show()
}
}
} else {
Alert(controller: self).show()
}
}
override func viewDidLoad() {
let newItemButton = UIBarButtonItem(title: "new item",
style: UIBarButtonItemStyle.Plain,
target: self,
action: Selector("showNewItem"))
navigationItem.rightBarButtonItem = newItemButton
}
@IBAction func showNewItem() {
let newItem = NewItemViewController(delegate: self)
if let navigation = navigationController {
navigation.pushViewController(newItem, animated: true)
} else {
Alert(controller: self).show()
}
}
func getMealFromForm() -> Meal? {
if nameField == nil || happinessField == nil {
return nil
}
let name = nameField!.text
let happiness = happinessField!.text.toInt()
if happiness == nil {
return nil
}
let meal = Meal(name: name, happiness: happiness!)
meal.items = selected
println("eaten: \(meal.name) \(meal.happiness) \(meal.items)")
return meal
}
@IBAction func add() {
if let meal = getMealFromForm() {
if let meals = delegate {
meals.add(meal)
if let navigation = self.navigationController {
navigation.popViewControllerAnimated(true)
} else {
Alert(controller: self).show(
message: "Unexpected error, but the meal was added.")
}
return
}
}
Alert(controller: self).show()
}
}
Segue o Código do NewItemViewController.swift:
import UIKit
protocol AddAnItemDelegate {
func addNew(item:Item)
}
class NewItemViewController: UIViewController {
let delegate:AddAnItemDelegate?
init(delegate:AddAnItemDelegate) {
self.delegate = delegate
super.init(nibName: "NewItemViewController", bundle: nil)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@IBOutlet var nameField:UITextField?
@IBOutlet var caloriesField:UITextField?
@IBAction func addNewItem() {
if nameField == nil || caloriesField == nil {
return
}
let name = nameField!.text
let calories = NSString(string: caloriesField!.text).doubleValue
let item = Item(name: name, calories: calories)
if delegate == nil {
return
}
delegate!.addNew(item)
if let navigation = navigationController {
navigation.popViewControllerAnimated(true)
}
}
}