2017-12-28 27 views
0

デリゲートメソッドがあります。テーブルビューのボタンを押すと別のビューコントローラとデータを渡しますが、動作していないようです。テーブルビューセル内で分割するメソッドを委譲する

func goToVC(uid: String) { //delegate method 
    performSegue(withIdentifier: "showVC", sender: self) //Do I need this 
} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    performSegue(withIdentifier: "showVC", sender: self) 
    self.tableView.deselectRow(at: indexPath, animated: true) 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "showVC" { 
     if let indexPath = tableView.indexPathForSelectedRow { 
      let guestVC = segue.destination as! GuestViewController 
      guestVC.ref = userArray[indexPath.row].ref 
     } 
    } 

答えて

0
class MainViewController: UIViewController { 

    // set the cell's delegate in the data source 
    // pass the object to the cell from the data source 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     cell.mainViewControllerDelegate = self 
     cell.object = someArray[indexPath.row] 

    } 

    // this is the method that gets called by the cell through the delegate 
    func pushToViewController(object: YourDataObject) { 

     let destination = SomeViewController() 
     destination.object = object 
     navigationController?.pushViewController(destination, animated: true) 

    } 

} 

class TheTableViewCell: UITableViewCell { 

    // create a delegate and a data object 
    var mainViewControllerDelegate: MainViewController? 
    var object: YourDataObject? 

    // this is the method that gets called when the button in the cell is tapped 
    @objc func buttonAction() { 

     mainViewControllerDelegate?.pushToViewController(object: object) 

    } 

} 

私は非常に初心者がInterface Builderを使用しないことをお勧めします。あなたがそれを早期に使用することが少なくなれば、あなたはもっと理解しやすくなります。 Interface Builderは、初心者のための愚かな金です。

+0

ありがとうございます。新しいView Controllerにリダイレクトすると、黒い画面が表示されます(エラーは発生しません)。これを解決する最良の解決策は何ですか? –

+0

デリゲートが正しく接続されているようです。まず、 'navigationController?.pushViewController(destination、animated:true)'を削除し、単に 'print(" great success ")'に出力することで確認してください。デリゲートが実際に動作している場合は、View BuilderをInterface Builderで通常どおりに操作します。私が与えた例はプログラマチックなアプリケーションですが、概念の変更はありません。 – slickdaddy

+0

うーん...デリゲートは「大成功」をプリントアウトしているようだ。デリゲート内の別のView Controllerにプッシュする方法はありますか? –

0

ここで委任方法は必要ありません。子ビューコントローラーから値を渡す必要がある場合は、デリゲートメソッドを使用できます。

あなたがしていることはまさに正しいことです。ストーリーボードにセグ識別子を正しく設定してください。

テーブルIBOutletをデフォルトのtableViewに設定しないと、そのテーブルの名前をtoDoTableのように設定してみると、簡単にデバッグできます。

関連する問題