0

I have some text in tableview cells which you will tap on it you will pass the name of the cell and also you will be pushed to another view.

       override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath as IndexPath, animated: true)

    let row = indexPath.row
    valueToPass = sections[row]
    print(valueToPass)
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if (segue.identifier == "fromWelcomeToSectionPage") {
        // initialize new view controller and cast it as your view controller
         let vc:sectionPage = segue.destination as! sectionPage
        vc.passedValue = valueToPass

    }
}

also I created code in another controller to check

    if passedValue == "Základy" {
        print("it works")
    }

This is how I trying to pass it. Variable valueToPass is global variable. Idk but when I'm printing it in didSelectRowAtIndexPath it's okey but in prepare it's nil.
After tapping on cell I've got unexpectedly found nil while unwrapping an Optional value

That's how I created variable in another view

var passedValue = String()
patrikbelis
  • 1,350
  • 2
  • 16
  • 35
  • 2
    `valueToPass = vc.passedValue` should be `vc.passedValue = valueToPass` – dan Oct 18 '16 at 16:09
  • Also, your tableview delegate method is using a signature from an older swift version and your prepare for segue method is using the Swift 3 signature. Are they both getting called? – dan Oct 18 '16 at 16:11
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – i_am_jorf Oct 18 '16 at 16:12
  • @dan it doesn't matter probably because it's still the same.. tried it.. yeah they getting called. – patrikbelis Oct 18 '16 at 16:12
  • You're sure your `didSelect` method is getting called? You put a breakpoint in it and it stops at the breakpoint when you select a row? – dan Oct 18 '16 at 16:21
  • just typed print("called") but sec i'll test it – patrikbelis Oct 18 '16 at 16:23
  • vc.passedValue = valueToPass not valueToPass=vc.passedValue ..just reverse – Gagan_iOS Oct 18 '16 at 16:26
  • check edited code and bolded text... still doesn't work – patrikbelis Oct 18 '16 at 16:31
  • I think problem in your segue – Mohammad Arifuzzaman Oct 18 '16 at 16:39
  • You removed the call to perform the segue from your table cell selection code. Is that because you have the segue hooked up between the cell itself and the new view controller in your storyboard? If so, prepare for segue will be called before the cell selection method. – dan Oct 18 '16 at 16:44
  • Yeah i have hooked up – patrikbelis Oct 18 '16 at 16:48

1 Answers1

1

Your prepareForSegue method is called before the didSelectRowAt method, so you can't rely on any logic you do in didSelectRow to help pass the data.

You can get the selected row in prepareForSegue and use it there to get the data and pass it along:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if (segue.identifier == "fromWelcomeToSectionPage") {
         // initialize new view controller and cast it as your view controller
         let vc = segue.destination as! sectionPage
         let selectedRow = tableView.indexPathForSelectedRow!.row
         vc.passedValue = sections[selectedRow]
    }
}
patrikbelis
  • 1,350
  • 2
  • 16
  • 35
dan
  • 9,695
  • 1
  • 42
  • 40