将请求数据从UIPageViewController传递到子视图

时间:2017-04-07 10:53:41

标签: ios swift request uipageviewcontroller pass-data

我的MainViewController是我的 UIPageViewController 。我想要显示2个FirstDataViewControllerSecondDataViewController的观看次数。

我想执行更新MainViewControllre的请求,然后将其传递给我的2个子视图。

getData是我的功能,其中包含来自请求的数据,然后更新MainViewController's

extension MainViewController {
    func getData() {
        getBasicData() { [weak self] (basicModel) in
            guard let strongSelf = self else { return }
            strongSelf.getExperienceData() { [weak self] (experienceModel) in
                guard let strongSelf = self else { return }

                let items = RequestItems(basicData: basicModel,
                                         experienceData: experienceModel)

                strongSelf.updateItems(items: items)
            }
        }
    }
}

MainViewController:

class MainViewController: UIPageViewController {

    var items: RequestItems

    let firstDataViewController: FirstDataViewController
    let secondDataViewController: SecondDataViewController

    let basicDataManager: APIManagerProtocol
    let experienceDataManager: APIManagerProtocol

    private(set) lazy var orderedViewControllers: [UIViewController] = {
        return [self.firstDataViewController, self.secondDataViewController]
    }()

    convenience init() {
        self.init(with: RequestItems())
    }

    init(with items: RequestItems) {

        self.items = items

        let apiManager = APIManager()
        basicDataManager = apiManager.createBasicDataManager()
        experienceDataManager = apiManager.createExperienceDataManager()

        self.firstDataViewController = FirstDataViewController(with: items)
        self.secondDataViewController = SecondDataViewController(with: items)

        super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)

        self.edgesForExtendedLayout = []
    }

    func updateItems(items: RequestItems) {
        self.items = items
    }
}

如何使用getData()函数首先更新MainViewController项,然后将数据传递给子视图?或者也许有更好的选择来做到这一点?

1 个答案:

答案 0 :(得分:0)

这不是一个好的解决方案。 “GetData”方法需要一些时间才能完全执行,因此您应该:

  • 在加载MainViewController之前执行“getData”,然后将“请求项”传递给它并更新你的视图。

但我认为您的MainViewController是应用程序中的第一个控制器,因此您应该:

  1. 将完成块添加到“getData”方法中:

    func getData(_ completion: [RequestItems] -> ()) {
    
        getBasicData() { [weak self] (basicModel) in
    
            guard let strongSelf = self else { return }
    
            strongSelf.getExperienceData() { [weak self] (experienceModel) in
    
                guard let strongSelf = self else { return }
    
                let items = RequestItems(basicData: basicModel,
                                     experienceData: experienceModel)
    
                completion(items)
            }
        }
    }
    
  2. 将“updateItems”方法添加到您的视图控制器(而不是在init方法中传递)

  3. 使用完成处理程序但在MainViewController init

    之外调用“getData”方法
    init(with items: RequestItems) {
        ...
        self.items = items
    
        self.firstDataViewController = FirstDataViewController()
        self.secondDataViewController = SecondDataViewController()
        ...
    }
    
    func viewDidLoad() {
        super.viewDidLoad()
    
        getBasicData { [weak self] (items) in 
            guard let strongSelf = self else { return }
    
    
            strongSelf.updateItems(items)
            strongSelf.firstDataViewController.updateItems(with: items)
            strongSelf.secondDataViewController.updateItems(with: items)
        }
    }
    
    func updateItems(items: RequestItems) {
        self.items = items
    }