gpt4 book ai didi

json - 使用 Swift + Alamofire + Swifty-Json 将动态数据传递给 UIScrollView

转载 作者:可可西里 更新时间:2023-11-01 01:37:25 24 4
gpt4 key购买 nike

我有一个 ViewController,它有一个 UIScrollView。 UIScrollView 加载三个 ViewControllers vc0, vc1, vc2 :

enter image description here

问题

在 ViewController 上,我使用 Alamofire 和 SwiftyJson 加载数据:

Alamofire.request(.GET, url, parameters: ["foo": "bar"])
.responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)

dispatch_async(dispatch_get_main_queue()) {
self.views = json["Views"].stringValue
print(self.views)
}

}
case .Failure(let error):
print(error)
}

}

Result from Print: 3603 which is correct.

我试过:

dispatch_async(dispatch_get_main_queue()) {
self.views = json["Views"].stringValue
vc0.views = views
}

我还尝试在 alamofire 调用中添加 vc0.views = views 并放入使 View 显示在 UIScrollView 中的代码:

    let vc0 = vc0(nibName: "vc0", bundle: nil)

vc0.views = self.views

self.addChildViewController(vc0)
self.displayReportView.addSubview(vc0.view)
vc0.didMoveToParentViewController(self)

let vc1 = vc1(nibName: "vc1", bundle:nil)

var frame1 = vc1.view.frame
frame1.origin.x = self.view.frame.size.width
vc1.view.frame = frame1

self.addChildViewController(vc1)
self.displayReportView.addSubview(vc1.view)
vc1.didMoveToParentViewController(self)

let vc2 = vc2(nibName: "vc2", bundle:nil)

var frame2 = vc2.view.frame
frame2.origin.x = self.view.frame.size.width * 2
vc2.view.frame = frame2

self.addChildViewController(vc2)
self.displayReportView.addSubview(vc2.view)
vc2.didMoveToParentViewController(self)

self.displayReportView.contentSize = CGSizeMake(self.view.frame.size.width * 3, self.view.frame.size.height - 66);

There are no errors in the console, it just does not display anything

附言

其他一切正常,获取数据并显示 ScrollView 并沿着它们滑动,唯一没有的是将数据从 ViewController 传递到 vc0vc1vc2 View 在 UIScrollView 中加载。

问题

如何将加载到 ViewController 的数据传递到 UIScrollView 中动态加载的 View (vc0vc1vc2).

项目文件

我一直在按照教程显示 View 等。这里是教程文件:https://www.veasoftware.com/s/Swipe-Navigation-Xcode-7.zip

最佳答案

在目标 ViewController 上,vc1 vc2 或 vc3 你如何显示数据?

如果你有:

override func viewDidLoad() {
super.viewDidLoad()

print(views)
}

尝试更改为 ViewDidAppear 方法

override func viewDidAppear(animated: Bool) {
print(views)
}

此外,在您的 Alamofire Call 中,执行:

dispatch_async(dispatch_get_main_queue()) {
self.views = json["Views"].stringValue
self.vc0.views = self.views
}

我还怀疑(通过你写问题的方式)你的 vc0、vc1、vc2 是在 alamofire 之后声明的,所以移动

let vc0 = vc0(nibName: "vc0", bundle: nil)

到顶部,就在你的类声明下面。

关于json - 使用 Swift + Alamofire + Swifty-Json 将动态数据传递给 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35246738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com