2016-09-17 86 views
2

似乎無法擺脫這種恐懼與xcode8的:實例成員「navigationItem」不能在類型中使用「MainViewController」

編譯失敗的消息:實例成員「navigationItem」不能在類型中使用「 MainViewController」

class MainViewController: UITableViewController { 

    private static var __once:() = { 
       let loginButton: UIBarButtonItem = UIBarButtonItem(title: nil, style: .done, target: self, action: nil) 
       MainViewController.navigationItem.rightBarButtonItem = loginButton 
     }() 

下面是調用此函數:

func setupRightBarButtonItem() { 
      struct Static { 
       static var onceToken: Int = 0 
      } 

      _ = MainViewController.__once 

      if (AWSIdentityManager.default().isLoggedIn) { 
       navigationItem.rightBarButtonItem!.title =  NSLocalizedString("Sign-Out", comment: "Label for the logout button.") 
       navigationItem.rightBarButtonItem!.action =  #selector(MainViewController.handleLogout) 
      } 
    } 
+0

你到底想幹什麼?這是行不通的,因爲消息說你不能在類類型上使用實例屬性 – Paulw11

+0

我試圖設置AWA SES並從他們的模板生成一些源代碼aws-my-sample-app- ios-swift v0.3工作。 – Bytor

+0

好吧,無論誰寫他們的模板,不應該被允許在Swift附近,如果這是他們正在推出。 – Paulw11

回答

2

它看起來像你正試圖創造合適的欄按鈕項目首次setupRightBarButtonItem被調用,但我不確定你想出了這個代碼。

該消息非常明確,navigationItem是一個實例屬性,您試圖將其用作類屬性。

爲什麼不使用更直接:

func setupRightBarButtonItem() { 

    if navigationItem.rightBarButtonItem == nil {  
     navigationItem.rightBarButtonItem = 
      UIBarButtonItem(title: nil, style: .done, target: self, action: nil)  
    } 

    if (AWSIdentityManager.default().isLoggedIn) { 
      navigationItem.rightBarButtonItem!.title =  NSLocalizedString("Sign-Out", comment: "Label for the logout button.") 
      navigationItem.rightBarButtonItem!.action =  #selector(MainViewController.handleLogout) 
    } 
    // You probably need an `else` clause here to update the button if the user isn't logged in? 
} 
相關問題