在iOS8上拆分视图控制器内的容器视图

时间:2014-11-20 07:31:44

标签: ios xcode6

我正在使用xcode 6.1(最新版本),在iPad上运行,而我的应用程序正在使用UISplitViewContoller。 主人和细节都嵌入了UINavigationController(正如Apple在他们的文档中所建议的那样)。我的问题与UISplitViewContoller的主要方面有关。 UISplitVewController architecture with UINavigationController and UISegmentControl

主人有UISegmentControl,允许用户在3种类型的视图“当前”,“镜头”和“会话”之间进行选择,以更改视图。我已经通过在主服务器中嵌入容器视图来实现这一点(并设置了正确的约束以将其调整为完整的主视图大小)。容器查看可以在故事板中拖出的标准对象:

Container view can be dragged from storyboard

我创建了一个代表master(在故事板中分配了类)的类HixFieldViewControllerMaster,并在故事板中连接了段控件和容器:

HixFieldViewControllerMaster class connected in storyboard

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函数中获取失败的任何日志)。我相信我可能在willMoveToParentViewControllerremoveFromSuperview ...或其他视图控制器内务管理方面做错了...

1 个答案:

答案 0 :(得分:1)

对于读这篇文章的人来说,我从来没有找到解决方案。 最后,我删除了分段控件(屏幕顶部)并使用选项卡控件(屏幕底部)。