![](/img/trans.png)
[英]How can I cover a UI element from the parent UIViewController by a container with another UIViewController in Swift?
[英]how can I access field/method from another UIViewController in Swift?
我有以下情况:
我有一个UIViewController,它嵌入了两个Container View
,一个是隐藏的,一个是可见的。 这两个Container View
都嵌入了另一个带有某些元素的UIViewController
。
我想在两个面板上都显示从Web服务获取的数据,但是我只想获取一次数据,然后根据用户当前所看到的面板以特定的方式解析它。
我有一种将数据提取为json的方法:
func loadInitialDataWithoutCluster() {
print("loadInitialData");
RestApiManager.sharedInstance.getRequests { json in
if let jsonData = json.array {
for requestJSON in jsonData {
dispatch_async(dispatch_get_main_queue(),{
if let request = SingleRequest.fromJSON(requestJSON){
//how can I add those single requests
// to a list/array/whatever here that will be
// accessible from both of two other panels?
}
})
}
}
}
}
然后当它获取所有数据并(以某种方式)将其分配给列表/数组时,如何从两个不同的面板中引用它?
这将是最佳编程实践的解决方案。 从技术上讲,您不应允许任何类直接操作数据,而应创建可以为您完成工作的函数。 我提供了一个读取和写入数组的函数,您可以根据需要进行操作。
class ArrayObject {
private var downloadedData = [1,2,3]
internal func readData() -> [Int] {
return downloadedData
}
internal func addData(number: Int) -> [Int] {
downloadedData.append(number)
return downloadedData
}
}
class genericControllerClass: UIViewController {
func currentData() {
let getUsers = ArrayObject().addData(15)
}
}
您可以使用简单的结构创建一个新文件来存储所需细节的数组,您可以从项目中的任何位置访问它。
struct Arrays {
static var downloadedData = [TheTypeYouNeedToStore]()
}
数据下载完毕后,可以仅使用Arrays.downloadedData.append()添加到此阵列。 然后,可以使用Arrays.downloadedData [0]访问和修改项目中的其他任何位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.