2016-09-07 9 views
0

UInavigationControllerを渡し、変数を新しいviewControllerに渡す最も良い方法は何ですか?私はどちらかを行う方法を知っていますが、同時に両方を行うことはありません。事前Swift - UInavigationControllerを渡して変数を渡す方法

でいただきありがとうございます、これはあなたがviewControllers[0]にアクセスして、あなたのメッセージビューコントローラ(コントローラのクラスにキャストし、あなたのナビゲーションコントローラのルートビューコントローラに値を渡すには、私の現在のコード

func(){ 

     let vc = storyboard?.instantiateViewControllerWithIdentifier("messagesViewController") as! UINavigationController 
     let posts = self.postList[indexPath.row] 

     //this is the var that i want to past 
     //vc.previousViewMessageId = posts.postKey 

     self.presentViewController(vc, animated: true, completion: nil) 


} 

答えて

0

私が正しくあなたを理解していれば、あなたは二VCを提示することができますビューコントローラを持っています。そしてこのVCはUINavigationControllerに埋め込まれています。方法を知らないものは、最初のVCからナビゲーションコントローラに、次に2番目のVCにデータを渡すことです。

ここではブルートフォースの解決策があります。それは美しいものではありませんが、とにかく動作します。

独自のUINavigationControllerサブクラスください:

class DataPasserController: UINavigationController { 
    var previousViewMessageId: SomeType? 
    override func viewDidLoad() { 
     if let vc = self.topViewController as? YourSecondViewController { 
      vc.previousViewMessageId = self.previousViewMessageId 
     } 
    } 
} 

今、あなたは、ストーリーボードにナビゲーションコントローラを追加DataPasserControllerにそのクラスを設定し、そのルートビューコントローラとしてそれに2つ目のVCを接続することができます。

yourDataPasserControllerInstance.previousViewMessageId = posts.postKey 

とインスタンスを提示:

は今、あなたはinstantiateViewControllerWithIdentifierを呼び出してDataPasserControllerのインスタンスを持っている、あなたがこれを行うことができたと!

+0

「apple」はUINavigationControllerを「self」内に埋め込むことはできません。私は通常、ビューを使用して独自のナビゲーションを作成しますが、それは「自己」内に埋め込むほうがずっと簡単です。 – pprevalon

+0

@pprevalon私はあなたの質問をよく理解していません。ストーリーボードのナビゲーションコントローラにVCを埋め込むことはできますか? – Sweeper

+0

これは素晴らしいアイデアですが、時には簡単なことを見落とすこともあります。 – pprevalon

0

です)previousViewMessageId性質を持っています

func() { 
    let messagesNC = storyboard?.instantiateViewControllerWithIdentifier("messagesViewController") as! UINavigationController 
    let messagesVC = messagesNC.viewControllers.first as! MessagesViewController 

    messagesVC.previousViewMessageId = postList[indexPath.row].postKey 

    presentViewController(messagesNC, animated: true, completion: nil) 
} 
0

単純にビューコントローラを表示していますが...ナビゲーションコントローラをスキップしています。

ナビゲーションコントローラー内に新しいビューコントローラーを表示するだけです。これを済ませたら正しく表示されます。変数vcを作成した後に変数を渡すこともできます。これは、新規のViewController内の変数(vc)(あなたが正しいです)だから、

vc.previousViewMessageId = posts.postKey 

を設定

self.navigationController?.pushViewController(vc, animated: false) 

これは、ナビゲーションコントローラ内の新しいのViewController(vc)を提示し...完了:

func(){ 

    let vc = storyboard?.instantiateViewControllerWithIdentifier("messagesViewController") as! MessagesViewController 
    let posts = self.postList[indexPath.row] 

    //this is the var that i want to past 
    vc.previousViewMessageId = posts.postKey 

    navigationController?.pushViewController(vc, animated: false) 
} 

ps。質問の一部ではありませんが、私はまだ言及すべきだと感じています...単語selfの使用は、必要なだけに残すべきです。言い換えれば、必要がないときには使用しないでください。例えばself.postList[indexPath.row] :) https://github.com/raywenderlich/swift-style-guide#use-of-self

関連する問題