ios - Pass request data from UIPageViewController to child views -
i have mainviewcontroller
uipageviewcontroller. ther 2 views firstdataviewcontroller
, seconddataviewcontroller
want show.
i want execute request updates items in mainviewcontrollre
, pass 2 child views.
getdata
function contains data request , updates mainviewcontroller's
items
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 } }
how use getdata()
function update mainviewcontroller items first , pass data child views? or maybe there better option this?
it's not solution. "getdata" method needs time execute should:
- execute "getdata" before load mainviewcontroller, pass "request items" , update yours views.
but assume mainviewcontroller first controller in application should:
add completion block "getdata" method:
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) } } }
add "updateitems" method yours view controllers (instead of passing in init method)
call "getdata" method completion handler outside of mainviewcontroller init
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 }
Comments
Post a Comment