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

安稳与你 提交于 2019-12-12 14:39:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!