ios – 如何使用扩展名设置viewController的搜索栏的委托?

我创建了导航自定义类,我想装饰它,我拿了NavDecoration. swift类并声明了下面代码中显示的扩展,我在这个函数中添加了搜索条形码,我想在这个扩展中设置搜索栏代理但是它的给定错误不能指定类型’UIViewController’来键入’UISearchBarDelegate
extension UINavigationController {
 func makeBlackNavigationbar (viewController: UIViewController,animated: Bool) {

 viewController.navigationController?.navigationBar.backgroundColor? = UIColor.blackColor()
    let add = UIBarButtonItem(barButtonSystemItem: .Add,target: viewController,action: "addTapped")
    let play = UIBarButtonItem(title: "Play",style: .Plain,action: "addTapped")
   viewController.navigationItem.rightBarButtonItems = [add,play]

    let left = UIBarButtonItem(barButtonSystemItem: .Add,action: "addTapped")
    viewController.navigationItem.leftBarButtonItems = [left]

       let searchBar = UISearchBar(frame: CGRectZero)
        searchBar.placeholder = "Search"
        searchBar.delegate = viewController
        viewController.navigationItem.titleView = searchBar
}}

解决方法

您必须符合UISearchBarDelegate协议:
extension UINavigationController : UISearchBarDelegate {
   ...
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...