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

前端之家收集整理的这篇文章主要介绍了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相关文章