简体   繁体   English

iOS Swift,将ViewController中的变量传递给同一ViewController中的类

[英]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.

相关问题 将数据从类传递到视图控制器的相同实例 - Passing data from a class to same instance of a viewcontroller 使用Swift将信息从ViewController传递到ViewController - Passing Information from ViewController to ViewController using Swift 从Swift中的另一个类访问ViewController中的变量 - Access variable in ViewController from a different class in Swift iOS Swift-选择到类-使用prepareForSegue将Int值从ViewController传递到类 - iOS Swift - segue to a class - passing Int value from a ViewController to a Class using prepareForSegue Swift:iOS-将数据从ViewController传递到第三类的Xib文件 - Swift: iOS - Passing data from ViewController to a xib file trough 3rd class 从viewcontroller类绘图 - drawing from viewcontroller class swift 在 object class 和 ViewController (Swift) 之间传递数据 - Passing data between an object class and a ViewController (Swift) 将一个ViewController中的managedObjectContext传递给Swift中的另一个ViewController? - Passing an managedObjectContext from one ViewController to another ViewController in Swift? Swift:将数据从ViewController中的tableView传递到另一个ViewController - Swift: Passing data from a tableView in a ViewController to another ViewController 将对象从一个ViewController传递到另一个ViewController很快就无效了 - Passing an object from one viewcontroller to another viewcontroller got nil in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM