问题
I am having an unexpected problem passing var's through Segue to a VC. Worked fine in Objective C and early version of Swift. Here is what I am doing:
I'm extracting a set of variables from a downloaded json file to populate a TableViewCell, then to pass the Cell displayed variables plus the other vars relating to that Cell selection to a DetailViewController. This is through prepare for Segue, setting up vars on the DetialViewController. Then destination.myVarToSend = myVar .....
That all works fine.
However, I want to pass on two of those variables from the DetailVC to a ThirdViewController. I set it all up as before, but I get a nil in the variable on the ThirdViewController?? Any ideas? Is there something I am missing?
The ThirdViewController is actually a MapView and the vars I am passing are Double. Attempts at passing those did not work so at the moment I am experimenting and trying to do the same with String vars. (Of course I changed the data in the json file to text strings).
I'm not posting any code at the moment as it is all standard stuff, but can do if required. :-)
Many thanks
回答1:
I just found the answer on a similar question which works fine for me, at : How do you pass data between view controllers in Swift?
final class Shared {
static let shared = Shared() //lazy init, and it only runs once
var stringValue : String!
var boolValue : Bool!
}
To set stringValue
Shared.shared.stringValue = "Hi there"
to get stringValue
if let value = Shared.shared.stringValue {
print(value)
}
来源:https://stackoverflow.com/questions/41937700/swift-3-passing-var-through-a-series-of-segues-viewcontrollers