-1

I have a segue that starts once a timer hits 00:00. The segue was created on the storyboard. Once it starts it goes to the next view with 4 different variables that are supposed to transfer but for some reason the variables all transfer as nil. The variables shouldn't be nil. minutesTotal comes from a slider, startValue and startFraction both come from pickers and current tank comes from another picker.

This is the code where the segue is performed.

@objc func updateTimer(){

        if seconds < 1 {
            timer.invalidate()
            isTimerRunning = false
            performSegue(withIdentifier: "timeToFinal", sender: self)
        } else {
            seconds -= 1
            timeLabel.text = timeString(time: TimeInterval(seconds))
        }

this is where i override the prepareSegue function

     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "timeToFinal"{
            if let finalFlowViewController = segue.destination as?  FinalFlowViewController{

                finalFlowViewController.currentTank = currentTank
                finalFlowViewController.startFraction = startFraction
                finalFlowViewController.startValue = startValue
                finalFlowViewController.time = minutesTotal

            }
        }
    }

Both of these come from ViewController.

  • Did you set breakpoints in your action functions for your pickers and switches? Breakpoints in prepareForSegue to check values? – Xcoder Dec 31 '17 at 22:41
  • Have you set a breakpoint in `prepare(for:sender:)` and stepped through to see what is happening? There are 2 `if` statements there that could be false. – Paulw11 Dec 31 '17 at 22:41
  • @Paulw11 exactly. OP, make sure you use breakpoints to debug the problem. – Xcoder Dec 31 '17 at 22:42
  • yes I know that both if statements return correctly on updateTime and on the second function I have a similar prepare function on another viewController that works fine but that one only has one variable that is being passed not 4 like this one. It goes through the segue just doesn't change the values on the next view controller. – Jose Hermosillo Dec 31 '17 at 22:49
  • Which four are nil, the ones in the current class or the ones that you are segueing to? – Xcoder Dec 31 '17 at 22:57
  • the ones that I am segueing to. – Jose Hermosillo Dec 31 '17 at 23:02
  • Also just in case this changes anything. The previous segues that the format for my prepare function worked for were segues that were fired when a button was pressed. I created those on the storyboard. Could the fact I called perform segue instead of creating a button link (although I did create a segue "timeToFinal" from View to View) be the reason? I know perform segue is supposed to call prepare but could there be a reason its not? @Xcoder – Jose Hermosillo Dec 31 '17 at 23:19
  • I think `perfomSegue` does call `prepareForSegue`, but did you set a breakpoint in `prepareForSegue` just to make sure? – Xcoder Jan 01 '18 at 01:31
  • https://stackoverflow.com/a/41887007/5461400 – Harshal Valanda Jan 01 '18 at 07:10
  • I set a breakpoint for prepareForSegue and it goes through the segue and assigns the values but when I try to use them on the next view controller they come back as nil for some reason when I try to use them in a function or to show as a label even though the variables do have the value. @Xcoder – Jose Hermosillo Jan 05 '18 at 01:06
  • Its finding nil when unwrapping the value even though the value has a value to be unwrapped – Jose Hermosillo Jan 05 '18 at 01:07
  • Just to make sure: the variables in the first view controller are **not** nil, correct? – Xcoder Jan 05 '18 at 01:25
  • Can you post your code from the view controller you're segueing to? – Xcoder Jan 05 '18 at 01:26
  • the controller I am segueing to only has a viewDidLoad() Override that has these same values shown on a label. @Xcoder – Jose Hermosillo Jan 05 '18 at 23:51
  • Are the variables in the first controller nil? – Xcoder Jan 06 '18 at 19:22

1 Answers1

1

I've created a gist that replicates what you've provided. You can adjust a slider and it sends the information through to FinalFlowViewController along with 3 other strings.

It all seems to be working so feel free to copy it. https://gist.github.com/CameronPorter95/874b3939c3b02075fee8126a5e29f462

Here's it in action:

https://media.giphy.com/media/26FeYPtlWN7X4S6uQ/giphy.gif

Cameron Porter
  • 801
  • 6
  • 15