2016-05-25 2 views
6

私はナビゲーションカスタムクラスを作成しました。私はそれを飾りたいと思っていました。私はNavDecoration.swiftクラスを取り、次のコードに示す宣言拡張を宣言しました。このバーコードもこの関数に追加しました。拡張子が、その与えるエラーはUISearchBarDelegateプロトコルに準拠しなければならない「UISearchBarDelegate拡張子を使用してviewControllerの検索バーの代理人を設定する方法は?

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, target: viewController, action: "addTapped") 
    viewController.navigationItem.rightBarButtonItems = [add, play] 

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

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

答えて

1

**私はほんの数行を変更することで行っていますコード**

extension UIViewController : UISearchBarDelegate { 

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, target: viewController, action: "addTapped") 
    viewController.navigationItem.rightBarButtonItems = [add, play] 
    let left = UIBarButtonItem(barButtonSystemItem: .Add, target: viewController, action: "addTapped") 
    viewController.navigationItem.leftBarButtonItems = [left] 

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

を入力するタイプ「のUIViewController」を代入することはできません。

extension UINavigationController : UISearchBarDelegate { 
    ... 
} 
+0

私はそれをすべての準備ができて、いつ私がsearchBar.delegate =のViewControllerのは私に同じエラーを与えることあなたが代理人としてのUIViewControllerの代わりUINAvigationControllerを割り当てる「UISearchBarDelegate – IOSDev

+1

を入力するタイプ「のUIViewController」を割り当てることができないことを好き行っています。拡張機能が上記のようなものであれば、searchBar.delegate = viewController.navigationControllerを設定する必要があります。 – stefos

関連する問題