Swift 3通过一系列Segues / ViewControllers传递var [英] Swift 3 passing var through a series of Segues / ViewControllers
问题描述
我将一个意想不到的问题通过Segue传递给VC。在目标C和早期版本的Swift中工作得很好。这是我在做什么:
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:
我从一个下载的json文件中提取一组变量来填充一个TableViewCell,然后将Cell显示的变量加上另外一个与该细胞选择相关的vars到DetailViewController。这是通过准备Segue,在DetialViewController上设置vars。然后destination.myVarToSend = myVar .....
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 .....
这一切都很好。
但是,我想将这两个变量从DetailVC传递给ThirdViewController。我像以前一样设置好了,但是在ThirdViewController的变量中我没有一个?有任何想法吗?有没有我缺少的东西?
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?
ThirdViewController实际上是一个MapView,我传递的vars是Double。尝试通过那些没有工作,所以在我正在尝试和尝试对String vars做同样的时刻。 (当然我将json文件中的数据更改为文本字符串)。
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. :-)
非常感谢
推荐答案
只是找到一个类似的问题的答案,对我来说可以正常工作:如何在Swift中的视图控制器之间传递数据?
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)
}
这篇关于Swift 3通过一系列Segues / ViewControllers传递var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!