2016-04-08 5 views
0

私はVocabアプリケーションを作成しようとしています。最初のViewControllerは静的なTableViewで、ユーザはあなたが覚えたい単語の日付を選択できます。すべてのday_vocabulary配列はこのViewControllerにあり、1つの配列には30のボキャブラリが含まれています。私は90日間vocabを持っています。prepareForSegueを使用して特定のデータを別のViewControlに渡す

ユーザが日付を選択すると、このViewCotrollerは30個のボキャブラリを表示する別のビューに配列を渡します。ここでは、特定のデータを別のViewControlに90個のセグを使用して渡そうとしましたが、全体的に見栄えが悪いです。また、私はこのようにdidSelectRowAtIndexPathを使用しようとした、

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    if indexPath.section == 0 && indexPath.row == 0 { 
     performSegueWithIdentifier("dayVocab1", sender: nil) 

    } 

} 

は、私は別の記事を読んでストーリーボードを使用せずに、セグエの識別子を設定することができないことに気づき、だから、結果は同じになります。

特定のデータを1つのセグだけで渡すことを知りたいと思います。

ここでは、準備のための別のコードを示します。 prepareForSegue FUNC

オーバーライド(:UIStoryboardSegue、送信者:セグエ?ANYOBJECT){

if (segue.identifier == "dayVocab1") { 

    let destViewController : ViewController = segue.destinationViewController as! ViewController 
     destViewController.vocabData = vocab_day1p1 
     destViewController.meaningData = meaning_day1p1 

    } 

    if (segue.identifier == "dayVocab2") { 

     let destViewController : ViewController = segue.destinationViewController as! ViewController 
     destViewController.vocabData = vocab_day1p2 
     destViewController.meaningData = meaning_day1p2 

    } 

}私は、送信者を使用して考えて

は、

解決の鍵となり、私は何を知っているだろう聞かせくださいこれに最適な解決策になります。

ありがとうございます。

答えて

1

私はあなたに90セグを維持するのはあまりにも多すぎることに同意します。あなたのコードから、私はあなたがその特定の日のvocabを正しく表示するために同じViewControllerを使用していると思いますか?したがって、同じViewControllerに90セグをルーティングする必要はありません。その日に別のボキャブを表示するには、別のデータを渡すだけです。ここで私はどうなるのかです:

があなたのdidSelectRowAtIndexPathでは「dayVocab

識別子を持つ新しいのViewControllerにあなたのtableViewから1つのセグエを作成し、コードは次のようになります。

if indexPath.section == 0 && indexPath.row == 0 { 
    performSegueWithIdentifier("dayVocab", sender: "dayVocab1") 
    } 

次にメソッドでは、このようにしてください:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if(segue.identifier == "dayVocab"){ 
     // do somechecking on the sender's type to make sure it is unwrapped properly 
     if let day = sender as! String { 

      if(day == "dayVocab1"){ 
       let destViewController : ViewController = segue.destinationViewController as! ViewController 
       destViewController.vocabData = vocab_day1p1 
       destViewController.meaningData = meaning_day1p1 
      }else if(day == "dayVocab2"){ 
       let destViewController : ViewController = segue.destinationViewController as! ViewController 
       destViewController.vocabData = vocab_day1p2 
       destViewController.meaningData = meaning_day1p2 
      } 

     } 
    } 
} 

もちろん、このコードはテストされておらず、保守性のニーズに合わせて自由に変更することができます。

+0

これ以上のセグは必要ありません! –

+0

それはあなたのために働いてうれしい。どうもありがとうございました。乾杯 – rach

関連する問題