2016-04-12 13 views
0

Neu bei der Verwendung von Protokollen hier.Standardwerte in Protokollerweiterungen

Ich habe eine Reihe von Viewcontrollern, die ein Protokoll namens MainNavBarItems verwendet.

Ich möchte folgende, aber mein Code unten eine Reihe von Fehlern trifft:

  • haben eine Variable namens RootView die Standardwerte falsch
  • der Lage sein, die Variable zu setzen, wenn initialzing die Viewcontroller
  • schaffen einen Selektor in den Protokollerweiterungsfunktionen

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 hat UIViewController zu verlängern. – ryantxr

+0

korrigiert. Das war in meinem Code. –

Antwort

0

Ihre ProfileViewController hat keine rootView Eigenschaft von mainNavBarItems Protokoll.

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

... und das Protokoll sollte Namen mit Großbuchstaben haben.

Protocol as Types