【发布时间】:2014-11-20 07:31:44
【问题描述】:
我使用的是 xcode 6.1(最新版本),在 iPad 上运行,我的应用程序使用的是 UISplitViewContoller。
master 和 detail 都嵌入了UINavigationController(正如 Apple 在其文档中所建议的那样)。我的问题与UISplitViewContoller 的主控端有关。
master 有一个UISegmentControl,允许用户在 3 种视图“current”、“shot”和“session”之间进行选择,从而改变 master 视图。我通过在主视图中嵌入一个容器视图来实现这一点(并设置了正确的约束以将其调整为完整的主视图大小)。容器视图是一个可以在storyboard中拖出的标准对象:
我创建了一个类HixFieldViewControllerMaster,它代表主控(在情节提要中分配了该类)并在情节提要中连接了段控件和容器:
HixFieldViewControllerMaster 派生自我的类 HixViewControllerContainer(因此我可以为其他类重用容器视图行为),其中包括函数 (void)loadViewWithClass:(Class) myClass
这个函数负责在容器视图中加载实际的视图控制器
@interface HixViewControllerContainer ()
@property (weak, nonatomic) UIView * containerView;
@property (strong, nonatomic) NSString * currentStoryBoardID;
@end
@implementation HixViewControllerContainer
#pragma mark - helper functions
-(void)loadViewWithClass:(Class)myClass
{
//if the class does not implement the required function assert (in debug)
if(![myClass respondsToSelector:@selector(instantiateFromStoryboard:)])
{
HixALog(@"%@ does not implement instantiateFromStoryboard",myClass);
return;
}
//if already loaded do nothing...
if([self.containerView class] == myClass) return;
//Remove the current Detail View Controller showed
if(self.currentContainerViewController)
{
[self.currentContainerViewController willMoveToParentViewController:nil];
[self.currentContainerViewController.view removeFromSuperview];
[self.currentContainerViewController removeFromParentViewController];
}
//create new vc
UIViewController * newVC=[myClass instantiateFromStoryboard:self.storyboard];
//set correct auto resize behaviour
newVC.view.translatesAutoresizingMaskIntoConstraints = NO;
//Add the detail controller as child of the container
[self addChildViewController:newVC];
//define the detail controller's view size
newVC.view.frame = self.containerView.frame;
//add the Detail controller's view to the Container's detail view and save a reference to the detail View Controller
[self.containerView addSubview:newVC.view];
UIView *myView=newVC.view;
//set correct constraits so resizing is ok
[self.containerView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|-0-[myView]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(myView)]];
[self.containerView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-0-[myView]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(myView)]];
//store new vc as the current one
self.currentContainerViewController = newVC;
//complete the add flow calling the function didMoveToParentViewController
[newVC didMoveToParentViewController:self];
}
这在 iOS 7.x 上运行良好,但在 iOS8 上表现异常;当用户使用段控件选择不同的视图时,有时主窗口会变为空白,有时会显示视图但未链接到代码(它仅显示在情节提要中绘制的内容但不执行底层代码)。
我确信instantiateFromStoryBoard 逻辑工作正常(我没有从 HixALog 函数获得任何失败的日志)。我相信我可能在 willMoveToParentViewController、removeFromSuperview... 或其他视图控制器内务方面做错了...
【问题讨论】: