2016-05-18 29 views
3

我想從我的Task類的HomeViewController中調用函數reloadTable。但我一直被拋出如何從另一個ViewController調用函數

在類型'HomeViewController'上使用實例成員'reloadTable';你的意思是使用'HomeViewController'類型的值嗎?

這是我的HomeViewController代碼:

import UIKit 
import Alamofire 
import SwiftyJSON 

class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
func reloadTable() { 
    self.displayTask.reloadData() 
} 
} 

這是我的任務等級:

import Foundation 
import Alamofire 
import SwiftyJSON 
class Tasks { 

static let sharedInstance = Tasks() 

var datas: [JSON] = [] 

func getTaskDetails(){ 
    Alamofire.request(.GET, Data.weeklyEndpoint).validate().responseJSON { response in 
     switch response.result { 
     case .Success(let data): 
      let json = JSON(data) 

      if let buildings = json.array { 
       for building in buildings { 
        if let startDate = building["start_date"].string{ 
         print(startDate) 
        } 
        if let tasks = building["tasks"].array{ 

         Tasks.sharedInstance.datas = tasks 

         HomeViewController.reloadTable() 

         for task in tasks { 
          if let taskName = task["task_name"].string { 
           print(taskName) 
          } 
         } 
        } 
       } 
      } 



     case .Failure(let error): 
      print("Request failed with error: \(error)") 
     } 
    } 

} 

// for prevent from creating this class object 
private init() { } 


} 

回答

0

許多其他答案指出,您試圖訪問靜態引用上的非靜態函數。

我會建議使用閉包,請考慮以下方面; 爲.Success.Failure創建一個閉包,這使您可以根據數據請求結果採取相應措施。 onError閉包定義爲可選項,這意味着您不必在'getTaskDetails`函數調用中實現onError,在您覺得需要錯誤信息的地方實現它。

func getTaskDetails(onCompletion: ([Task]) ->(), onError: ((NSError) ->())? = nil) { 

    Alamofire.request(.GET, Data.weeklyEndpoint).validate().responseJSON { 
     response in 

     switch response.result { 
      case .Success(let data): 
      let json = JSON(data) 

      if let buildings = json.array { 
       for building in buildings { 
        if let startDate = building["start_date"].string{ 
         print(startDate) 
        } 
        if let tasks = building["tasks"].array{ 

         Tasks.sharedInstance.datas = tasks 

         onCompletion(tasks) 
        } 
       } 
      } 

      case .Failure(let error): 
      print("Request failed with error: \(error)") 
      onError?(error) 
     } 
    }  
} 

從你HomeViewController

// Call from wherever you want to reload data. 
func loadTasks(){ 

    // With error handling. 
    // Fetch the tasks and reload data. 
    Tasks.sharedInstance.getTaskDetails({ 
     tasks in 

     self.displayTask.reloadData() 
    }, onError: { 
     error in 

     // Handle error, display a message or something. 
     print(error) 
    }) 


    // Without error handling. 
    // Fetch the tasks and reload data. 
    Tasks.sharedInstance.getTaskDetails({ 
     tasks in 

     self.displayTask.reloadData() 
    }) 
} 

Basics

Closures

+0

嗨@Laffen你爲什麼把onCompletion :([任務]) - >()?我不確定這是如何工作的 – noobdev

+0

我建議你看看我提供的'Closures'鏈接,文檔將詳細解釋什麼/如何以及爲什麼。你可以在你想要包含閉包的函數上定義'onCompletion';你可以從那個函數中調用'onCompletion',在那裏返回所請求的數據。當你調用'getTaskDetails'時,你定義了'onCompletion'的主體,這是當你從定義的函數中調用閉包時,告訴閉包該做什麼的地方。這有點毛茸茸的... – Laffen

0

製作類的任務,因爲結構,使功能getTaskDetails靜態和嘗試調用函數getTaskDetails( )在HomeViewController中。在這個函數的結果中使用realoadTable()

-1

在HomeViewController中,您已經將reloadTable()定義爲實例方法而不是類函數。您應該通過將HomeViewController的實例製作爲HomeViewController()來調用reloadTable()。替換

HomeViewController.showLeaderboard() 
HomeViewController().showLeaderboard() 

希望它有幫助。快樂編碼。

+0

肯定會有效? – raki

+0

沒有嘗試但邏輯上是的。順便說一句,我個人更喜歡這種情況下的通知模式。 – luckyShubhra

+0

它不起作用,因爲視圖控制器有一個HomeViewController實例,但是當你調用HomeViewController()時,它會創建另一個實例,它與viewCotroller的實例不同。 – raki

0

在這種情況下,reloadTable()是一種實例方法。你不能通過類名來調用它,你必須爲HomeViewController創建對象,然後你必須使用該對象調用該方法。

但是在這種情況下無需使用HomeViewController對象直接調用該方法。

添加的通知觀察員爲你HomeViewController

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeViewController.reloadTable), name:"ReloadHomeTable", object: nil)

添加上述線在你的HomeViewController

viewDidLoad方法:您可以通過使用 NSNotification

使用NSNotification以另一種方式做到這一點

現在在您的中將HomeViewController.reloadTable()替換爲 class

+0

不要忘記刪除觀察者。 – Laffen

+0

但我想知道在哪裏刪除觀察者? viewDidUnload現在不可用。但有時候,即使視圖消失,我們也需要更新數據。 – raki

+0

當你想從堆棧中刪除視圖控制器時,這是你應該刪除該視圖的觀察者。在'dismissViewController(_ :)'或類似的東西里面。 – Laffen

相關問題