问题
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)
}
}
回答1:
#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.
来源:https://stackoverflow.com/questions/43162043/uibarbuttonitem-argument-of-selector-cannot-refer-to-local-function-swift