Swift 3 passing var through a series of Segues / ViewControllers

拥有回忆 提交于 2019-12-25 09:27:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!