0

I've got some fairly simple code, and am trying to figure out why the uibarbuttonitem isn't triggering the selector:

TableView's viewDidLoad method:

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    self.clearsSelectionOnViewWillAppear = false

    // this line adds a button to top right
    let addNewChecklistItemButton = UIBarButtonItem(barButtonSystemItem: .add,
                                                    target: nil,
                                                    action: #selector(self.addNewChecklistItemToDataModel))

    self.navigationItem.rightBarButtonItem = addNewChecklistItemButton
}

Method called:

@objc func addNewChecklistItemToDataModel() {
    print("adding new item...")

    <...>
}

The button is being added (was not visible before I put this code in) and when I press it the button click animates but my console doesn't show the printed text.

After reading UIBarButtonItem selector not working, I'm wondering if the fact that my UITableViewController is embedded in both a UITabViewController and UINavigationViewController is effecting the button's scope? If not, anyone see something I'm missing?

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
Mercutio
  • 1,152
  • 1
  • 14
  • 33

1 Answers1

2

Target shouldn't be nil

 let addNewChecklistItemButton = UIBarButtonItem(barButtonSystemItem: .add,
                                                target: self,
                                                action: #selector(self.addNewChecklistItemToDataModel))
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87