ios - childViewControllers is empty after calling addChildViewController -
i have question regarding add/remove childviewcontrollers containerview.
i have uiviewcontroller named "containerviewcontroller" contains containerview, , containerview has uiviewcontroller named "containerlinkviewcontroller". have 4 other uiviewcontrollers added or removed "containerlinkviewcontroller" whenever user clicks 1 of tab button placed @ bottom of "containerviewcontroller". storyboard looks below.
note custom segues connected 4 uiviewcontrollers empty segue nothing show visual connection in storyboard.
so first add first tab viewcontroller "containerlinkviewcontroller" childviewcontroller using below code in viewdidload().
let firsttabviewcontroller = self.storyboard?.instantiateviewcontroller(withidentifier: "firsttabviewcontroller") as! firsttabviewcontroller self.addchildviewcontroller(firsttabviewcontroller) firsttabviewcontroller.view.frame = self.view.bounds self.view.addsubview(firsttabviewcontroller.view) firsttabviewcontroller.didmove(toparentviewcontroller: self)
after initial setup, whenever user tabs on 1 of tab buttons, send signal "containerviewcontroller" uiviewcontroller added new childviewcontroller "containerlinkviewcontroller" , switching old childviewcontroller , new childviewcontroller using below code.
func swapviewcontrollers(from : uiviewcontroller, : uiviewcontroller) { from.willmove(toparentviewcontroller: nil) self.addchildviewcontroller(to) to.view.frame = self.view.bounds self.transition(from: from, to: to, duration: 1.0, options: uiviewanimationoptions.transitioncrossdissolve, animations: nil) { finished in from.view.removefromsuperview() from.removefromparentviewcontroller() to.didmove(toparentviewcontroller: self) } }
before calling "swaptviewcontrollers()", take old childviewcontroller doing following.
let oldchildviewcontroller = self.childviewcontrollers.last
my problem "oldchildviewcontroller" nil when click 1 of tabs first time right after initial setup. function "swaptviewcontrollers()" not called because self.childviewcontrollers empty.
i have been struggling few days , not find clear answer solve this. decided ask here might cause this.
any help?? thanks.
note : using swift 3. maybe answer question since seems lots of things changed fro swift 2.
Comments
Post a Comment