swift - How to pass value from navigationController to TabBarController -
i have login project. first view controller navcontroller , need pass user data tabbarcontroller have 3 navigationcontrollers.
i tried this.
let opennewvc = self.storyboard?.instantiateviewcontrollerwithidentifier("mainnavid") as! uitabbarcontroller //opennewvc.token = token! self.navigationcontroller?.pushviewcontroller(opennewvc, animated: true)
you can pass data first view controller view controllers embedded below in uitabbarcontroller
.
uitabbarcontroller -> uinavigationcontroller -> uiviewcontroller
you need traverse viewcontrollers
instance of uitabbarcontroller
, uinavigationcontroller
in order instance of uiviewcontroller
. once instance of 'uiviewcontroller' embedded uitabbarcontroller
, can assign data required.
for example
if let tabbarcontroller = self.storyboard?.instantiateviewcontrollerwithidentifier("mainnavid") as? uitabbarcontroller { // need view controllers array tabbarcontrollers uinavigationcontrollers if let navigationcontrollers = tabbarcontroller.viewcontrollers as? [uinavigationcontroller] { navigationcontroller in navigationcontrollers { //now need viewcontrollers navigationcontroller stack, let viewcontrollers = navigationcontroller.viewcontrollers //now can assing desired value in viewcontrollers, assuming need assign same value in viewcontroler viewcontroller in viewcontrollers { } } } }
the best way pass data in kind of architecture using singleton, assume created class session
member variable token
.
class session { //singleton static let sharedinstance = session() //token assign , can use through out application var token : string? = nil }
now, can assign token while pushing uitabbarcontroller
.
let opennewvc = self.storyboard?.instantiateviewcontrollerwithidentifier("mainnavid") as! uitabbarcontroller session.sharedinstance.token = token //opennewvc.token = token! self.navigationcontroller?.pushviewcontroller(opennewvc, animated: true)
same token can use within uiviewcontroller
override func viewdidload() { super.viewdidload() if let token = session.sharedinstance.token { //now have assigned token } }
Comments
Post a Comment