在适应新的iOS 11 UISearchController
中的NavigationItem
后,我遇到了一些问题。iOS 11上的UIToolbar按钮无法调用的操作UISearchController
我已将UIBarButtonItem
添加到UISearchController
的搜索栏的工具栏中。此按钮在单击时调用一个函数(作为UIBarButtonItem
构造函数中的动作参数传递)。
在iOS 11之前,搜索栏已附加到tableHeaderView
,并且此工作完美(并且仍然有效)。当按钮被点击时,该功能被调用。
但是,在iOS 11中,即使实现相同,也不会调用该函数。
任何想法可能是错的?或者是iOS 11中的错误?
private func setupSearchController() {
...
let toolbar = UIToolbar()
toolbar.sizeToFit()
// Create bar button item with image.
let qrBarButton = UIBarButtonItem(image: #imageLiteral(resourceName: "QR Code"), style: .plain, target: nil, action: #selector(didPressQRToolbarButton))
// Add the new button.
toolbar.items = [qrBarButton]
searchController.searchBar.inputAccessoryView = toolbar
// If the device is on iOS 11, use the "native" search bar placement.
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
// Don't use the large title in the navigation bar.
navigationController?.navigationBar.prefersLargeTitles = false
} else {
// Handled in subclasses.
}
}
/// Action for the QR toolbar button
func didPressQRToolbarButton(sender: Any) {
...
// NOT CALLED
}
创建栏按钮项目时,您将'nil'作为目标。只要传递'self'作为目标,那么你的代码将无任何改变地工作。 并且在iOS的所有版本中都需要传递目标,否则按钮将不知道在Tap事件中传递的位置和对象。 – Rishi