[英]iOS Swift, Passing a variable from ViewController to a class in the same ViewController
I have a ViewController (A), which takes the output (quantity) of a swift class "QuantityMatrix { var qMatrix = [["1", "2", "3"], ["4", "5", "6"]] // returns quantity}" and performs some action. 我有一个ViewController(A),它取一个swift类的输出(数量)“QuantityMatrix {var qMatrix = [[”1“,”2“,”3“],[”4“,”5“,” 6“]] //返回数量}”并执行一些操作。
The code works fine if I provide the qMatrix to the RateMatrix Class. 如果我将qMatrix提供给RateMatrix类,则代码可以正常工作。 But, now I want to generate the variable qMatrix from other View Controller (B) and use it in the QuantityMatrix class.
但是,现在我想从其他View Controller(B)生成变量qMatrix并在QuantityMatrix类中使用它。
I am not getting how to pass the qMatrix from the View Controller (B). 我没有得到如何从视图控制器(B)传递qMatrix。 can anyone help?
有人可以帮忙吗?
ex: 例如:
Class ViewControllerA: UIViewController {
var qMatrix = QuantityMatrix()
// uses the 'quantity' returned from QuantityMatrix class to perform some action
}
Class QuantityMatrix {
var qMatrix:[[String]] = [["1", "2", "3"], ["4", "5", "6"]]
//
.
.
return quantity
}
The above works fine, but now instead of directly using the [["1", "2", "3"], ["4", "5", "6"]] values, I am generating these values from another viewcontroller (B). 以上工作正常,但现在不是直接使用[[“1”,“2”,“3”],[“4”,“5”,“6”]]值,而是从另一个生成这些值viewcontroller(B)。 For this, I need to pass the variable generated in B controller to QuantityMatrix.
为此,我需要将B控制器中生成的变量传递给QuantityMatrix。 Can anyone tell how?
谁能说出来怎么样?
I am new. 我是新的。 So please explain.
所以请解释一下。 Thanks.
谢谢。
You will need to provide a reference from from View Controller A to View Controller B, then it can simply ask for the values by calling a func. 您需要提供从View Controller A到View Controller B的引用,然后它可以通过调用func来简单地询问值。
The best place to do this is often in prepareForSegue, though it will depend on how you're opening the views. 执行此操作的最佳位置通常在prepareForSegue中,但这取决于您打开视图的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.