うちのいぬ Tech Blog

Tech Blog of Uchinoinu/My dog

Watch log without any thoughts... What I forgot when I add action to button of navigation

I tried to add button in navigation bar. I tried to add action to the button. But I forgot to set UIButton sender to argument of action method.

This is my first experience to get trouble in this level. So I look back me so deeply.

In Logs,

2015-12-14 14:34:38.377 HogeController onClickBarButtonEdit:]: unrecognized selector sent to instance 
2015-12-14 14:34:38.389  *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Fuga.HogeViewController onClickBarButtonEdit:]: unrecognized selector sent to instance 0x7f8025c01a50'

Logs said the reason.... I should have watched Logs without any hot thoughts.

Below is succeeded example.

func viewDidLoad() {
・
・
    let barButtonEdit = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Edit, target: self, action: "onClickBarButtonEdit:")
    let barButtons: NSArray = [barButtonEdit]
    self.navigationItem.setRightBarButtonItems(barButtons as? [UIBarButtonItem], animated: true)
    }
・
・
}
  
func onClickBarButtonEdit(sender:UIButton) {
    print("hoge")
}