Boa tarde! Assim que eu inseri o conteúdo dessa atividade no código, começou a dar erro. Não faço ideia de qual seja o problema hah
ViewController
@IBOutlet var tableView : UITableView?
func add(_ item: Item) {
items.append(item)
if let table = tableView{
table.reloadData()
}
}
override func viewDidLoad() {
let newItemButton = UIBarButtonItem(title: "new item", style: UIBarButtonItemStyle.plain, target: self, action: #selector(showNewItem))
navigationItem.rightBarButtonItem = newItemButton
}
@objc func showNewItem(){
let newItem = NewItemViewController(delegate: self) //poderia passar algo dentro tipo um putExtra
if let navigation = navigationController{
navigation.pushViewController(newItem, animated: true)
}
}
//numero de linhas na table
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
//oq vai ter em cada celula da linha da table
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = indexPath.row
let item = items[row]
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier:nil)
cell.textLabel!.text = item.name
return cell
}
NewItemViewController
var delegate:AddAnItemDelegate?
init(delegate: AddAnItemDelegate){
super.init(nibName: "NewItemViewController", bundle: nil)
self.delegate = delegate
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@IBOutlet var nameField : UITextField?
@IBOutlet var caloriesField : UITextField?
@IBAction func addNewItem(){
let name = nameField!.text
let calories = Double(caloriesField!.text!)
if (name == nil || calories == nil || delegate == nil) {
return
}
let item = Item(name: name!, calories: calories!)
delegate!.add(item)
if let navigation = navigationController{
navigation.popViewController(animated: true)
}
}
AddAnItemDelegate
import Foundation
protocol AddAnItemDelegate {
func add(_ item: Item)
}
Grata desde já pela ajuda!