XCode Runtime Error, unrecognized selector sent to instance

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)
  }
Answered by Claude31 in 733647022

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)
  }

Your selector

    navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(UIPushBehavior.addItem(_:)))

should be changed to

    navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(UIPushBehavior.addItem(sender:)))

because you have declared:

  func addItem(sender: UIBarButtonItem) {

What you have would work with a func declared as:

  func addItem(_ sender: UIBarButtonItem) {

Thank you for the reply, I have added this line but now I am getting this error, Type 'UIPushBehavior' has no member 'addItem(sender:)'

navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(UIPushBehavior.addItem(sender:)))
     
Accepted Answer

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)
  }

Perfect that worked great thank you!

Now when I click save I get the same error appears "[Shopping_List.AddItemViewController save:]: unrecognized selector sent to instance ..."

//
// AppDelegate.swift
// Shopping List
//
// Created by Deepak Prasad on 1/10/22.
//

import UIKit

protocol AddItemViewControllerDelegate {
  func controller(controller: AddItemViewController, didSaveItemWithName name: String, andPrice price: Float)
}

class AddItemViewController: UIViewController {
   
  @IBOutlet var nameTextField: UITextField!
  @IBOutlet var priceTextField: UITextField!
   
  var delegate: AddItemViewControllerDelegate?
   
  // MARK: -
  // MARK: View Life Cycle
  override func viewDidLoad() {
    super.viewDidLoad()
  }
   
  // MARK: -
  // MARK: Actions
  @IBAction func cancel(sender: UIBarButtonItem) {
    dismiss(animated: true, completion: nil)
  }
   
  @IBAction func save(sender: UIBarButtonItem) {
    if let name = nameTextField.text, let priceAsString = priceTextField.text, let price = Float(priceAsString) {
      // Notify Delegate
      delegate?.controller(controller: self, didSaveItemWithName: name, andPrice: price)
       
      // Dismiss View Controller
      dismiss(animated: true, completion: nil)
    }
  }

}
XCode Runtime Error, unrecognized selector sent to instance
 
 
Q