2016-04-22 31 views
0

現在、ボタンはTableViewControllerに移動するように設定されていますが、そのTableViewControllerをTabBarControllerに埋め込みたいと決めました。 UITabBarControllerに渡す際にエラーが発生しています。タイプ 'UIViewController'の値にJSONという名前のメンバ 'jsonfile'がありません

タイプのコードの最後の行で
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if (segue.identifier == "showListSegue") { 
     let tabBarController = segue.destinationViewController as! UITabBarController 
     tabBarController.selectedIndex = 0 // choose which tab is selected 
     let des = tabBarController.selectedViewController! 
     des.jsonfile = self.jsonfile 
    } 
} 

des.jsonfile = self.jsonfile、私はエラーを取得しています...

値は 'のUIViewController' 私は何のメンバーのjsonfile "

を持っていませんjsonファイルをUITabBarControllerに埋め込まれたTableViewControllerに渡そうとしています。これはどうすればできますか?私はこの変数をTableViewControllerに渡していましたが、今ではこのTabBarControllerをミックスに投げたので、すべて混乱しています。

また、TabBarcontroller用のCocoaファイルを作成し、変数var jsonfile : JSON!を設定しようとしましたが、それも機能しませんでした。 (それは私が渡したい私のTableViewControllerの変数です)助けてください。ありがとうございました。

+1

'UIViewController'を' TableViewController'にキャストし、そこから 'jsonfile'プロパティにアクセスする必要があります。 – rmaddy

+0

例がありますか? –

+0

UITabBarControllerをViewControllerのクラス名に変更するだけです。ビルドされていることを確認する(cmd + b) – Emptyless

答えて

1

selectedViewControllerは、メンバーがjsonFileのタイプであることをコンパイラーに知らせる必要があります。また、実行時に実際に存在し、正しいクラスであることを確認する必要があります。

class JSONDisplayController: UIViewController { 
    var jsonfile: String 
} 

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if (segue.identifier == "showListSegue") { 
     guard let tabBarController = segue.destinationViewController as? UITabBarController else { 
      preconditionFailure("Unexpected destination.") 
     } 

     tabBarController.selectedIndex = 0 // choose which tab is selected 
     guard let des = tabBarController.selectedViewController as? JSONDisplayController else { 
      preconditionFailure("Unexpected selection.") 
     } 

     des.jsonfile = jsonfile 
    } 
} 
関連する問題