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

Pass Dynamic Data to UIScrollView using Swift + Alamofire + Swifty-Json

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

问题

在 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 并放入使视图显示在 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

P.S

其他一切正常,获取数据并显示滚动视图并沿着它们滑动,唯一没有的是将数据从 ViewController 传递到在 UIScrollView[= 中加载的 vc0vc1vc2 视图58=].

问题

如何将 ViewController 上加载的数据传递给 UIScrollView 中动态加载的视图 (vc0 ,vc1,vc2).

项目文件

我一直在按照教程显示视图等。这里是教程文件: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)

到顶部,就在您的 class 声明下方。