ios - How to obtain reference to a View Controller from UITabBarViewController -
edit: off base in approach. i'm trying pass data 1 vc another. source vc not controlled tab bar, can't reference destination vc through it. attempting reference destination vc thorough storyboard, data still nil on destination vc. i'm using core data , wish pass managedobjectcontext vc. here code:
...... { try self.managedobjectcontext.save() appuserid = user.userid! } catch { fatalerror("error: \(error)") } } } self.passmanagedobjectcontext() } func passmanagedobjectcontext() { let profiletableviewcontroller = uistoryboard(name: "profile", bundle: nil).instantiateviewcontroller(withidentifier: "profileviewcontroller") as! profiletableviewcontroller profiletableviewcontroller.managedobjectcontext = self.managedobjectcontext } passing data appdelegate other viewcontrollers referencing tabbarviewcontrollers stack so: let tabbarcontroller = window!.rootviewcontroller as! uitabbarcontroller if let tabbarviewcontrollers = tabbarcontroller.viewcontrollers { let profilenavcontroller = tabbarviewcontrollers[4] as! uinavigationcontroller let profiletableviewcontroller = profilenavcontroller.topviewcontroller as! profiletableviewcontroller profiletableviewcontroller.managedobjectcontext = context } } .....
end edit i'm trying same 1 viewcontroller accessed through tab bar (no segue). code above not recognize window!.rootviewcontroller
how can reference root uitabbarcontroller?
uiviewcontroller
has built-in optional property tabbarcontroller
, inside view controller can access tabbarcontroller
so:
if let tabbarcontroller = tabbarcontroller { //get controller need //tabbarcontroller.viewcontrollers //and whatever need }
here current list of properties available in 'getting other related view controllers' section of apple's uiviewcontroller documentation:
Comments
Post a Comment