uisplitviewcontroller - iOS - Split View Controller - How do I get a pointer (reference) to the Detail View Controller from inside the Master View Controller? -
ios - split view controller - how pointer (reference) detail view controller (the bigger right one) inside master view controller (the smaller left one)?
my gut tell me main split view controller should have reference detail view controller , own master view controller, can't figure out how it.
any thoughts?
split view controllers do have references master , detail view controllers, via viewcontrollers
property.
in ios 7.x , below, viewcontrollers
array should have 2 view controller objects in it. first object master view controller , second object detail view controller.
in ios 8.0 , above, viewcontrollers
array has @ least 1 view controller object in – master (or "primary") view controller. if second view controller object in array, detail (or "secondary") view controller. when split view controller collapsed, master view controller in array, , when expanded contain both master , detail view controllers.
you can use splitviewcontroller
property of view controllers split view controller, , there use viewcontrollers
property access either master or detail view controllers, so:
swift:
let mastervc = self.splitviewcontroller?.viewcontrollers.first let detailvc = (self.splitviewcontroller?.viewcontrollers.count > 1) ? self.splitviewcontroller?.viewcontrollers[1] : nil
objective-c:
uiviewcontroller *mastervc = [self.splitviewcontroller.viewcontrollers firstobject]; uiviewcontroller *detailvc; if (self.splitviewcontroller.viewcontrollers.count > 1) { detailvc = self.splitviewcontroller.viewcontrollers[1]; }
the splitviewcontroller
property works walking view controller hierarchy , trying find split view controller calling view controller in. if view controller not in split view controller, property nil
. works same navigationcontroller
, tabbarcontroller
view controller properties.
you can make master , detail view controllers easier access using extension in swift (or category in objective-c) on uisplitviewcontroller
, (replacing xx_
's own prefix if you're using objective-c):
swift:
extension uisplitviewcontroller { var primaryviewcontroller: uiviewcontroller? { return self.viewcontrollers.first } var secondaryviewcontroller: uiviewcontroller? { return self.viewcontrollers.count > 1 ? self.viewcontrollers[1] : nil } }
objective-c:
// uisplitviewcontroller+childviewcontrolleraccess.h @interface uisplitviewcontroller (childviewcontrolleraccess) @property (nonatomic, readonly) uiviewcontroller *xx_primaryviewcontroller; @property (nonatomic, readonly) uiviewcontroller *xx_secondaryviewcontroller; @end // uisplitviewcontroller+childviewcontrolleraccess.m @implementation uisplitviewcontroller (childviewcontrolleraccess) - (uiviewcontroller *)xx_primaryviewcontroller { return self.viewcontrollers.firstobject; } - (uiviewcontroller *)xx_secondaryviewcontroller { return self.viewcontrollers.count > 1 ? self.viewcontrollers[1] : nil; } @end
you can make use of these properties so:
swift:
func somefunctioninsomeviewcontrollerclass { // primary , secondary view controllers if // view controller in split view controller. // these nil if view controller not // descendant of split view controller. var primaryvc = self.splitviewcontroller?.primaryviewcontroller var secondaryvc = self.splitviewcontroller?.secondaryviewcontroller // them primaryvc?.title = "this primary vc" secondaryvc?.title = "this secondary vc" }
objective-c:
#import "uisplitviewcontroller+childviewcontrolleraccess.h" [...] - (void)somemethodinsomeviewcontrollerclass { // primary , secondary view controllers if // view controller in split view controller. // these nil if view controller not // descendant of split view controller. uiviewcontroller *primaryvc = self.splitviewcontroller.xx_primaryviewcontroller; uiviewcontroller *secondaryvc = self.splitviewcontroller.xx_secondaryviewcontroller; // them primaryvc.title = @"this primary vc"; secondaryvc.title = @"this secondary vc"; }
Comments
Post a Comment