2016-05-25 109 views
6

Gezinme özel sınıfı oluşturdum ve onu dekore etmek istedim, NavDecoration.swift sınıfını aldım ve aşağıdaki kodda gösterilen uzantıyı ilan ettim, bu işlevde arama çubuğu kodunu da ekledim, buradaki arama çubuğu temsilcisini ayarlamak istedim uzatma ama onun vererek hata Sen UISearchBarDelegate protokole uymak zorunda 'UISearchBarDelegateEklenti kullanarak viewController arama çubuğunun temsilci nasıl atanır?

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 
}} 

cevap

1

** Ben sadece birkaç satırını değiştirerek yapmış kod **

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

yazmak için türü 'UIViewController' arasında atanamıyor:

extension UINavigationController : UISearchBarDelegate { 
    ... 
} 
+0

Hazır hepsini yapmış ve ben searchBar.delegate = viewController onun beni vererek aynı hata Sen delege olarak yerine UINavigationController ait UIViewController'ın atama 'UISearchBarDelegate – IOSDev

+1

yazmak için türündeki 'UIViewController' atanamıyor böyle bir söz vardır. Eğer uzantınız yukarıdaki gibi ise, searchBar.delegate = viewController.navigationController ayarını yapmanız gerekir. – stefos

İlgili konular