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)
}
}
#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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With