iOS - Split View Controller - How do I get a pointer (reference) to the Detail View Controller (the bigger right one) from inside the Master View Controller (the smaller left one)?
iOS - 拆分视图控制器 - 如何从主视图控制器(较小的左侧控制器)内部获取指向详细视图控制器(较大的右侧控制器)的指针(参考)?
My gut tell me the Main Split View Controller should have a reference to Detail View Controller and to my own Master View Controller, but I can't figure out how to get it.
我的直觉告诉我主拆分视图控制器应该引用细节视图控制器和我自己的主视图控制器,但我无法弄清楚如何获得它。
Any thoughts?
有什么想法吗?
2 个解决方案
#1
50
Split view controllers do have references to their master and detail view controllers, via the viewControllers
property.
拆分视图控制器通过viewControllers属性确实引用了它们的主视图控制器和详细视图控制器。
In iOS 7.x and below, the viewControllers
array should have exactly two view controller objects in it. The first object is the master view controller and the second object is the detail view controller.
在iOS 7.x及更低版本中,viewControllers数组中应该只有两个视图控制器对象。第一个对象是主视图控制器,第二个对象是详细视图控制器。
In iOS 8.0 and above, the viewControllers
array has at least one view controller object in it – the master (or "primary") view controller. If a second view controller object is in the array, then it is the detail (or "secondary") view controller. When the split view controller is collapsed, only the master view controller is in this array, and when expanded it will contain both the master and the detail view controllers.
在iOS 8.0及更高版本中,viewControllers数组中至少有一个视图控制器对象 - 主(或“主”)视图控制器。如果第二个视图控制器对象在数组中,则它是详细信息(或“辅助”)视图控制器。折叠拆分视图控制器时,只有主视图控制器位于此数组中,扩展后它将包含主视图控制器和详细视图控制器。
You can use the splitViewController
property of all view controllers to get your split view controller, and from there use the viewControllers
property to access either your master or detail view controllers, like so:
您可以使用所有视图控制器的splitViewController属性来获取拆分视图控制器,并从那里使用viewControllers属性访问主视图控制器或详细视图控制器,如下所示:
Swift:
迅速:
let masterVC = self.splitViewController?.viewControllers.first
let detailVC = (self.splitViewController?.viewControllers.count > 1) ? self.splitViewController?.viewControllers[1] : nil
Objective-C:
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 by walking up the view controller hierarchy and trying to find any split view controller that the calling view controller is in. If the view controller is not in a split view controller, then the property is nil
. It works the same as the navigationController
and tabBarController
view controller properties.
splitViewController属性通过向上走视图控制器层次结构并尝试查找调用视图控制器所在的任何拆分视图控制器来工作。如果视图控制器不在拆分视图控制器中,则属性为nil。它的工作方式与navigationController和tabBarController视图控制器属性相同。
You can make the master and detail view controllers easier to access using an extension in Swift (or a category in Objective-C) on UISplitViewController
, like so (replacing all the xx_
's with your own prefix if you're using Objective-C):
您可以使用UISplitViewController上的Swift(或Objective-C中的类别)中的扩展来更轻松地访问主视图和详细视图控制器,就像这样(如果您使用的是Objective-C,则用您自己的前缀替换所有xx_):
Swift:
迅速:
extension UISplitViewController {
var primaryViewController: UIViewController? {
return self.viewControllers.first
}
var secondaryViewController: UIViewController? {
return self.viewControllers.count > 1 ? self.viewControllers[1] : nil
}
}
Objective-C:
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 then make use of these properties like so:
然后,您可以像这样使用这些属性:
Swift:
迅速:
func someFunctionInSomeViewControllerClass {
// Get the primary and secondary view controllers if
// this view controller is in a split view controller.
// These will be nil if this view controller is not a
// descendant of a split view controller.
var primaryVC = self.splitViewController?.primaryViewController
var secondaryVC = self.splitViewController?.secondaryViewController
// Do something with them
primaryVC?.title = "This is the primary VC"
secondaryVC?.title = "This is the secondary VC"
}
Objective-C:
Objective-C的:
#import "UISplitViewController+ChildViewControllerAccess.h"
[...]
- (void)someMethodInSomeViewControllerClass
{
// Get the primary and secondary view controllers if
// this view controller is in a split view controller.
// These will be nil if this view controller is not a
// descendant of a split view controller.
UIViewController *primaryVC = self.splitViewController.xx_primaryViewController;
UIViewController *secondaryVC = self.splitViewController.xx_secondaryViewController;
// Do something with them
primaryVC.title = @"This is the primary VC";
secondaryVC.title = @"This is the secondary VC";
}
#2
1
Create a property in your UISplitViewController subclass:
在UISplitViewController子类中创建一个属性:
var _detailViewController: UIViewController? {
get {
if viewControllers.count > 1 {
return viewControllers[1] as? UIViewController
}
return nil
}
}
According to Apple's documentation, this should sometimes return nil, but in my experience, it always returns the detail view controller, regardless of state.
根据Apple的文档,这应该有时返回nil,但根据我的经验,无论状态如何,它总是返回详细视图控制器。
Also, do not call this property "detailViewController" instead of "_detailViewController" - Apple is apparently already using that name under the hood, and it will mess with your UI.
此外,不要将此属性称为“detailViewController”而不是“_detailViewController” - Apple显然已经在引擎盖下使用该名称,并且它会弄乱您的UI。
UISplitViewController is really hokey and needs a lot of cleanup and corrected documentation...
UISplitViewController非常好,需要大量的清理和纠正文档......
#1
50
Split view controllers do have references to their master and detail view controllers, via the viewControllers
property.
拆分视图控制器通过viewControllers属性确实引用了它们的主视图控制器和详细视图控制器。
In iOS 7.x and below, the viewControllers
array should have exactly two view controller objects in it. The first object is the master view controller and the second object is the detail view controller.
在iOS 7.x及更低版本中,viewControllers数组中应该只有两个视图控制器对象。第一个对象是主视图控制器,第二个对象是详细视图控制器。
In iOS 8.0 and above, the viewControllers
array has at least one view controller object in it – the master (or "primary") view controller. If a second view controller object is in the array, then it is the detail (or "secondary") view controller. When the split view controller is collapsed, only the master view controller is in this array, and when expanded it will contain both the master and the detail view controllers.
在iOS 8.0及更高版本中,viewControllers数组中至少有一个视图控制器对象 - 主(或“主”)视图控制器。如果第二个视图控制器对象在数组中,则它是详细信息(或“辅助”)视图控制器。折叠拆分视图控制器时,只有主视图控制器位于此数组中,扩展后它将包含主视图控制器和详细视图控制器。
You can use the splitViewController
property of all view controllers to get your split view controller, and from there use the viewControllers
property to access either your master or detail view controllers, like so:
您可以使用所有视图控制器的splitViewController属性来获取拆分视图控制器,并从那里使用viewControllers属性访问主视图控制器或详细视图控制器,如下所示:
Swift:
迅速:
let masterVC = self.splitViewController?.viewControllers.first
let detailVC = (self.splitViewController?.viewControllers.count > 1) ? self.splitViewController?.viewControllers[1] : nil
Objective-C:
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 by walking up the view controller hierarchy and trying to find any split view controller that the calling view controller is in. If the view controller is not in a split view controller, then the property is nil
. It works the same as the navigationController
and tabBarController
view controller properties.
splitViewController属性通过向上走视图控制器层次结构并尝试查找调用视图控制器所在的任何拆分视图控制器来工作。如果视图控制器不在拆分视图控制器中,则属性为nil。它的工作方式与navigationController和tabBarController视图控制器属性相同。
You can make the master and detail view controllers easier to access using an extension in Swift (or a category in Objective-C) on UISplitViewController
, like so (replacing all the xx_
's with your own prefix if you're using Objective-C):
您可以使用UISplitViewController上的Swift(或Objective-C中的类别)中的扩展来更轻松地访问主视图和详细视图控制器,就像这样(如果您使用的是Objective-C,则用您自己的前缀替换所有xx_):
Swift:
迅速:
extension UISplitViewController {
var primaryViewController: UIViewController? {
return self.viewControllers.first
}
var secondaryViewController: UIViewController? {
return self.viewControllers.count > 1 ? self.viewControllers[1] : nil
}
}
Objective-C:
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 then make use of these properties like so:
然后,您可以像这样使用这些属性:
Swift:
迅速:
func someFunctionInSomeViewControllerClass {
// Get the primary and secondary view controllers if
// this view controller is in a split view controller.
// These will be nil if this view controller is not a
// descendant of a split view controller.
var primaryVC = self.splitViewController?.primaryViewController
var secondaryVC = self.splitViewController?.secondaryViewController
// Do something with them
primaryVC?.title = "This is the primary VC"
secondaryVC?.title = "This is the secondary VC"
}
Objective-C:
Objective-C的:
#import "UISplitViewController+ChildViewControllerAccess.h"
[...]
- (void)someMethodInSomeViewControllerClass
{
// Get the primary and secondary view controllers if
// this view controller is in a split view controller.
// These will be nil if this view controller is not a
// descendant of a split view controller.
UIViewController *primaryVC = self.splitViewController.xx_primaryViewController;
UIViewController *secondaryVC = self.splitViewController.xx_secondaryViewController;
// Do something with them
primaryVC.title = @"This is the primary VC";
secondaryVC.title = @"This is the secondary VC";
}
#2
1
Create a property in your UISplitViewController subclass:
在UISplitViewController子类中创建一个属性:
var _detailViewController: UIViewController? {
get {
if viewControllers.count > 1 {
return viewControllers[1] as? UIViewController
}
return nil
}
}
According to Apple's documentation, this should sometimes return nil, but in my experience, it always returns the detail view controller, regardless of state.
根据Apple的文档,这应该有时返回nil,但根据我的经验,无论状态如何,它总是返回详细视图控制器。
Also, do not call this property "detailViewController" instead of "_detailViewController" - Apple is apparently already using that name under the hood, and it will mess with your UI.
此外,不要将此属性称为“detailViewController”而不是“_detailViewController” - Apple显然已经在引擎盖下使用该名称,并且它会弄乱您的UI。
UISplitViewController is really hokey and needs a lot of cleanup and corrected documentation...
UISplitViewController非常好,需要大量的清理和纠正文档......