UIBarButtonItem - '#selector' 的参数不能引用本地函数 - Swift 3

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

我在创建会调用 handleSearch 函数的导航按钮时遇到问题。我收到一条消息 Argument of '#selector' cannot refer to local function。任何帮助,将不胜感激!

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 只能引用顶层函数,不能引用局部函数(一个在另一个函数中)。

只需将 handleSearch 移出 viewDidLoad 即可解决您的问题。