2016-04-13 4 views
0

オブジェクトをテーブルビューから詳細ビューに渡そうとしています。私はrevealviewcontrollerフレームワークを使ってメニューをスライドさせています。したがって、私はtableviewからrevealviewcontrollerへのsegueを作成する必要があり、ここから別のものを最終的なdetailviewcontrollerに作成する必要があります。間にrevealviewcontrollerを持つテーブルビューからオブジェクトへのパス

私は詳細ビューでオブジェクトを設定できません - そのようにするにはどうすればよいですか?

これは使用されるコードです:

if segue.identifier == "communityDetailSegue" { 

    // Get the cell that generated this segue. 
    if let selectedCommunityCell = sender as ? UITableViewCell { 

    let destination = segue.destinationViewController as!CommunityViewController 

    if let communityIndex = self.tableView.indexPathForCell(selectedCommunityCell) { 

     destination.community = self.communitiesOfCurrentUser[communityIndex.row] 
     print(self.communitiesOfCurrentUser[communityIndex.row].name) 
    } 

    } 
} 

そして、これは例外です。

は、型の値をキャストすることができませんでした「SWRevealViewController」(0x10027b9f0)に「CommunityViewController」

答えて

1

セグエの宛先VCがSWRevealViewControllerないCommunityViewControllerあるので、あなたは、エラーが発生します。あなたの問題を解決する1つの方法は二つのステップで値を渡すことであろう

まず、prepareForSegue()にあなたは(あなたが例えばMyRevealViewController、このいずれかのサブクラスが必要です)SWRevealViewControllerに値を渡します。

if segue.identifier == "communityDetailSegue" { 

    // Get the cell that generated this segue. 
    if let selectedCommunityCell = sender as ? UITableViewCell { 

    let destination = segue.destinationViewController as! MyRevealViewController 

    if let communityIndex = self.tableView.indexPathForCell(selectedCommunityCell) { 

     destination.community = self.communitiesOfCurrentUser[communityIndex.row] 
     print(self.communitiesOfCurrentUser[communityIndex.row].name) 
    } 
    } 
} 

その後、MyRevealViewControllerであなたはすぐにそれが設定されているとして値を渡すことができます

class MyRevealViewController : SWRevealViewController { 

    // Let's assume this is the outlet to your final VC: 
    IBOutlet let communityViewController: CommunityViewController! 

    var community: YourCommunityType { 
     didSet { 
      if let communityVC = self.communityViewController { 
       communityVC.community = self.community 
      } 
     } 
    } 
} 
+0

ありがとうございました!私はあなたの助言に従った - 今私は "MyRevealViewController(0x11b8fc)にタイプ 'SWRevealViewController'(0x2235004)の値をキャストできませんでした。サブクラスを実装するときに必要なその他のことは? – Burkart

+0

はい、SWRevealViewControllerの代わりにInterface BuilderのViewControllerのクラスとしてMyRevealViewControllerを設定する必要があります。 – Lasse

+0

ビューのinitメソッドを追加する必要がありますが、今は動作します。これに助けてくれてありがとう! – Burkart

関連する問題