Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIBarButtonItem - Argument of '#selector' cannot refer to local function - Swift 3

I am having a problem with creating a nav button that would call handleSearch function. I receive a message Argument of '#selector' cannot refer to local function. Any help would be appreciated!

override func viewDidLoad() {
   // various code

   func setupNavBarButtons() {
       let searchImage = UIImage(named:"search_icon")
       let searchBarButtonItem = UIBarButtonItem(image: searchImage, style: .plain, target: self, action: #selector(handleSearch(sender:)))
        navigationItem.rightBarButtonItems = [searchBarButtonItem]

    }

    func handleSearch(sender: UIBarButtonItem) {
        print(123)
    }
}
like image 924
Luke Avatar asked Dec 13 '22 23:12

Luke


1 Answers

#selector can only reference a top-level function, not a local function (one inside another function).

Simply move handleSearch out of viewDidLoad and your problem will be solved.

like image 145
rmaddy Avatar answered Dec 16 '22 14:12

rmaddy