Hi everyone,
I am new to xcode and am getting this runtime error when I click on the add button for my application. Any help would go along away, thank you!
Error: "Exception NSException * "-[Shopping_List.ListViewController addItems:]: unrecognized selector sent to instance 0x7f8fe6507b50" 0x000060000263b690"
override func viewDidLoad() {
super.viewDidLoad()
// Set Title
title = "Items"
// Register Class
tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: CellIdentifier)
// Create Add Button
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(UIPushBehavior.addItem(_:)))
// Create Edit Button
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: Selector(("editItems:")))
}
// MARK: -
// MARK: Add Item View Controller Delegate Methods
func controller(controller: AddItemViewController, didSaveItemWithName name: String, andPrice price: Float) {
// Create Item
let item = Item(name: name, price: price)
// Add Item to Items
items.append(item)
// Add Row to Table View
tableView.insertRows(at: [IndexPath(row: (items.count - 1), section: 0)], with: .none)
// Save Items
saveItems()
}
// MARK: -
// MARK: Actions
func addItem(sender: UIBarButtonItem) {
performSegue(withIdentifier: "AddItemViewController", sender: self)
}
func editItems(sender: UIBarButtonItem) {
tableView.setEditing(!tableView.isEditing, animated: true)
}
I don't know what is UIPushBehavior that you had in your initial code…
So try (if addItem is defined in the class where this is called)
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addItem(sender:)))
You should also mark the func as @objc:
@objc func addItem(sender: UIBarButtonItem) {
performSegue(withIdentifier: "AddItemViewController", sender: self)
}
@objc func editItems(sender: UIBarButtonItem) {
tableView.setEditing(!tableView.isEditing, animated: true)
}