2016-04-12 20 views
0

ここではプロトコルを使用することが初めてです。プロトコル拡張のデフォルト値

私は、mainNavBarItemsというプロトコルを使用する一連のviewcontrollerを持っています。

私は次のようになるが、以下の私のコードでは、誤差の範囲に当たっている:

  • 偽デフォルトはViewControllerを
  • をinitialzing時に変数を設定することができrootViewという変数を持っています
  • プロトコル拡張機能

protocol mainNavBarItems { 
    var rootView:Bool {get set} 
    func gotoRootProfileView() 
    init() 
    init(rootView:Bool) 
} 

extension mainNavBarItems{ 

    // how do i set the default to be true? 
    init(rootView: Bool) { 
    self.init() 
    self.rootView = rootView 
    } 


    func addMainNavBarItems(){ 
    let s = self as! UIViewController 
    // ERROR: using a string, but not sure how to define a selector in this case 
    s.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: s, action: "gotoRootGlobalView") 
    } 

} 


class ProfileViewController: UIViewController, mainNavBarItems{ 
    func gotoRootProfileView() { 
    // does stuff 
    } 
} 

// How do i set the boolean for rootview here? 
let vc:ProfileViewController = UIStoryboard.getController("Profile", vc: "profileVC") 
// ERROR: profileviewcontroller does not have a rootview 
vc.rootView = true 
AppDelegate.rootGotoWithMainNav(vc) 
にセレクタを作成
+0

ProfileViewControllerは、UIViewControllerを拡張する必要があります。 – ryantxr

+0

が訂正されました。それは私のコードにあった。 –

答えて

0

ProfileViewControllerrootViewプロパティがmainNavBarItemsのプロトコルではありません。

class ProfileViewController: UIViewController, mainNavBarItems{ 
    var rootView = false // this is default 
    func gotoRootProfileView() { 
     // does stuff 
    } 
} 

...プロトコルには大文字の名前を付ける必要があります。

Protocol as Types

関連する問題