0

In Swift 3, I'd like to pass the string contained in a cell of a UITableView into the subsequent View Controller.

As a note, I read several answers to this that seem to be specific to earlier versions of Swift, so I wanted to reopen. As an example, the last solution here looked straightforward, but I can't get it to work.

In TableViewController, I create a string of an audio file's name and date posted with this JSON extraction:

if let shows_list = json as? NSArray
{
    for i in 0 ..< data_list.count
    {
        if let shows_obj = shows_list[i] as? NSDictionary
        {
            let episode_name = shows_obj["episode"] as? String
            let episode_date = shows_obj["date"] as? String
            TableData.append(episode_date! + " | " + episode_name!)
            let testString = TableData.append(episode_date! + " | " + episode_name!)

// the part above runs and will add the string into each cell. now here, I want to pass the value of that string into a UILabel called episodeTitle in the target view controller

            func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
                if let indexPath = self.tableView.indexPathForSelectedRow {
                    let destinationVC = segue.cellPasser
                    EpisodeViewController.text = testString //this is the string in second view controller
                }
            }
        }
    }
}

This is throwing two errors:

  1. Value of type 'UIStoryboardSegue' has no member 'cellPasser'

In the Storyboard, I have added an identifier to the Show segue called 'cellPasser.' My belief is I should be able to call that here but it's being flagged.

  1. EpisodeViewController.episodeTitle = testString

I have an outlet in EpisodeViewController that is called episodeTitle, so I am wondering if there is a preferred way other than this to pass strings into an element like that.

I'm new to Swift -- hoping someone sees a better way to do this.

Edit: In case useful, here's the View Controller linked from the segue.

class EpisodeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    @IBOutlet var episodeTitle: UILabel!
5
  • YOu have done totally wrong code.So pls explain what actually u tring to do. Commented Jan 18, 2018 at 6:03
  • You should try using segue.destination at the place of segue.cellPasser. EpisodeViewController.text shouldn't work either, it should be destinationVC.text if there is any text property added to EpisodeViewController. I strongly recommend to go through a tutorial explaining segue like this. Commented Jan 18, 2018 at 6:09
  • 1. cellParser is identifier so first you need to check the condition for that i.e segue.identifier == cellParser . segue does not have any property cellParser you need to check against the identifier property of segue. 2. Once the condition is met you can get the instance of destination view controller like, let destinationVC = segue.destination as? EpisodeViewController, then you can assign your string to second view controller property like, destinationVC. episodeTitle = testString. Commented Jan 18, 2018 at 6:25
  • @Yogi: This is helpful. That clears up the first error. Since I have an outlet for a UILabel, any clue how I can direct destinationVC.text to connect to it? Only error now is that the view controller 'has no member text.' Commented Jan 18, 2018 at 9:35
  • You need to take a property of type String in class EpisodeViewController. and then use it to save the value passed from the previous view controller. Example: If I declare a property var strEpisodeTitle in class EpisodeViewController and save the value as destinationVC.strEpisodeTitle then I can use the value stored in strEpisodeTitle to set to label in class EpisodeViewController. Commented Jan 18, 2018 at 10:14

1 Answer 1

1

Here are steps to pass value from TableViewController to next ViewController :

  1. in TableViewController You should declare a didSelectRowAt method.
  2. Declare a method prepare for segue
  3. Do not forget to declare a variable in second view Controller.

1.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: "Identifier", sender: indexPath)
    }

2.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Identifier" {
        let vc = segue.destination as! NameOfYourViewController
        vc.variableInSecondVc = variableInTableView
    }
 }

3.

var variableInSecondVc = ""
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.