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!