2017-08-12 14 views
2

我在故事板與tableview中這樣的視圖控制器:子類有IBOutlet中泰伯維斯威夫特

enter image description here

它的階級是這樣的(我聯繫稱爲 'tabella' 的IBOutlet中實現代碼如下):

class RisultatiRicerca: UIViewController , UITableViewDataSource , UITableViewDelegate{ 

    var codeSearch = 0 

    @IBOutlet public var tabella : UITableView! 


    override func viewDidLoad() { 

     super.viewDidLoad() 
     self.tabella.estimatedRowHeight = 50 
     self.tabella.rowHeight = UITableViewAutomaticDimension 
     self.tabella.delegate = self 
     self.tabella.dataSource = self 
     self.tabella.reloadData() 

    } 

    @available(iOS 2.0, *) 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cella = tableView.dequeueReusableCell(withIdentifier: "risu") as! CellaRisultato 

     return cella 

    } 

    @available(iOS 2.0, *) 
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 

     return 10 
    } 

} 

我的問題是這樣的:

我要創建的這個子類上面,比如這個:

class TuttiRicerca: RisultatiRicerca { 

    override func viewDidLoad() { 

     super.viewDidLoad() 
     self.codeSearch = 1 
    } 
} 

但是當我提出的TuttiRicerca我得到這個錯誤(「tabella」是無):

enter image description here

這就像子犯規情節板中的鏈接實現代碼如下。 你能幫我嗎?

+0

你要麼失敗掛鉤出口在故事板,或者,如果你以編程的方式實例化了這個視圖控制器,你做得不對,所以檢查IB的出口,如果你以編程方式轉換到這個場景,那麼我們用你的代碼 – Rob

+0

在故事板中,我只有控制器關於' RisultatiRicerca'類,它的出口是鏈接的。我沒有控制器關於它的子類 – gianni

+0

istanciate'TuttiRicerca'控制器我只是這樣做的:public let viewController1:TuttiRicerca = TuttiRicerca ) – gianni

回答

1

如果您需要IBOutlet的參考資料,您不能只參考TuttiRicera()。它如何知道你試圖使用的故事板中的哪個場景(即使你只有一個場景)?

你需要給故事板場景中的「故事板ID」,然後通過分鏡進行實例化(例如storyboard.instantiateViewController(withIdentifier: "..."),不TuttiRicera()。見instantiateViewController(withIdentifier:)

+0

我用instantiateViewController(withIdentifier :)做了,但我有一個問題:我在故事板中有'RisultatiRicerca'控制器,並通過以下方法實例化它:storyboard?.instantiateViewController(withIdentifier:「ric」)。但我希望它是'TuttiRicerca'類型,但如果我投了,應用程序崩潰(當然) – gianni

+0

我不知道我是否解釋得很好,但我會盡量避免爲故事板中的每個子類創建一個視圖控制器 – gianni

+0

讓我們[在聊天中繼續討論](http://chat.stackoverflow.com/rooms/151801/discussion-between-rob-and-gianni)。 – Rob