0
votes

I am trying to make Vocab app. first ViewController is a static tableView, user can choose date of word that you want to memorize. All day_vocabulary arrays are in the this ViewController, one array contains 30 vocabularies, I have vocab for 90 days.

when user chooses a date, This ViewCotroller passes an array to the another View that will display 30 vocabularies. Here, I tried to pass a specific data to another ViewControl with using 90 segues, It doesn't look good at all. Also I tried to use didSelectRowAtIndexPath like this,

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

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

    }

}

I realized that I can't set identifier of segue without using storyboard by reading another post, So the result will be same.

I want to know that passing specific data with using only one segue.

Here is my another code for prepareFor segue.

override func prepareForSegue(segue: UIStoryboardSegue, sender: 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

    }

}

I think using sender is the key of solution,

please let me know what would be the best solution for this.

Thank you.

1

1 Answers

1
votes

I agree with you that 90 segues is a little too much to maintain. From your codes, I can safely assume that you are using the same ViewController to display that specific day's vocab right? So you don't really need 90 segues routing you to the same ViewControllers. You just need different data passed to it so it will display different vocabs for that day. Here is what I would do:

Create one segue from your tableView to the new ViewController with identifier "dayVocab"

In your didSelectRowAtIndexPath, the codes would look like this:

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

Then in prepareForSegue:segue:sender method, do it like this:

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
           }

         }
     }
}

Of course, this codes have not been tested and feel free to modify it to suit the needs of maintainability.