1

I'm having trouble accessing and using the variable the value I got from a popover

First time ever asking a question on here and fairly new to programming so please be gentle. My program has a popup that displays a date for the user to select, and then that date is passed back to the main view controller. I tested this aspect to make sure the data was being passed back to the vc by getting the program to display the date in a label using callbacks and delegation (I tested both ways), and it worked fine, but what I'm trying to do is use that value taken from the popup, placing it into a variable, and using it for further calculations in other functions.

I tried to take change the protocol function have a return value from originally (value: String) -> (), to (value: String) -> (String)

but then realized this wouldn't work when I try to access that function from say the viewDidLoad function I don't have an input value like if I did this inside of viewDidLoad

    date = popupValueSelected(value: ???) // throws an error because it doesn't know about the delegate

This is my delegate protocol code:

    protocol PopupDelegate {
        func popupValueSelected(value:String)
    }

Here is the code used in my main view controller to get the Data from the popover, which all works fine

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
            let popup = segue.destination as! DatePopupViewController 
            popup.delegate = self
    }

    func popupValueSelected(value: String) {
            date = value // how to access this value from somewhere else??
}

All I want to do is use the value that I from the popover in another separate function. How would I pass that value somewhere else if I can't return it? I've sifted through answers on here and saw someone ask a similar question, but the first answer I saw was to use delegation, but no further explanation. I don't want to display that value or use it right away, I only want to use it for another calculation, that will be displayed when the user clicks a different button.

I'm sorry if this doesn't make sense as I'm having a difficult time explaining the issue, hence the throwaway account, though I feel like there's something so simple that I'm missing.

I appreciate all the feedback I can get!

Ariana
  • 13
  • 3
  • It’d be helpful if you edited your question with an eye toward simple things like punctuation, capitalization, and missing words. People are more likely to put effort into answers if you make your question easy to read and understand. – Caleb Apr 23 '19 at 03:30
  • [This question](https://stackoverflow.com/q/37244146/643383) illustrates what you’re trying to do. There are lots of others on the same topic. [Here’s another.](https://stackoverflow.com/q/29462380/643383) – Caleb Apr 23 '19 at 03:34
  • you can call to your `separate` function inside the delegate method or assign that `value` to a internal variable and then you can use it wherever you want inside the class. – caldera.sac Apr 23 '19 at 04:06
  • If I call to another function from the delegate, I would have to pass the value to it, and then if I want to use that result again I could just make that function have a return, correct? Seems like a longer way to do it, though I didn’t think of that so thank you. – Ariana Apr 23 '19 at 12:33

1 Answers1

1

Unless I’m missing something you seem to be 90% of the way there. You’ve got popupValueSelected getting a value. If you store the value off to a class level variable you can then use it later. Let me know if I’m not understanding your issue fully.

Justin
  • 1,318
  • 9
  • 12
  • Okay, I think I see what you are saying. I would initialize a variable in the class and after the delegate function is called, assigning the new value to that variable will show if I use it somewhere else? – Ariana Apr 23 '19 at 12:28