iOS切换故事板中的embedded视图

我一直试图弄清楚这一切,我知道它应该能够完成,但对iOS开发使用Objective-C而不是Appcelerator新手我有新手问题。

我试图完成的是在其他视图中embedded视图,但是能够通过编程来切换embedded的视图。 我正在使用故事板。 当我试图使用视图容器它显示附加的视图,但我不能附加多个视图到故事板中的容器。 我画了一个图像,但对我的烦恼,我不能在这里张贴图像,因为我没有足够的“代表”点,所以我张贴在我的网站: http : //static.c6gx.com/images/iphone -embedded式的视图,diagram.jpg

后来我想在视图容器内看起来像是push segue,但是首先我只是希望能够在embedded视图1,2,3等之间切换。我应该使用视图容器来完成这个还是其他一些types的控制器? 那么我将如何调用过渡来改变视图?

感谢您提供任何帮助。

您可以像使用容器视图控制器一样切换视图控制器。 我做了一个项目,添加了一个容器视图(实际上是2个,但是我们只是在这里处理一个),并将这些代码添加到在容器视图中拖动的embedded式控制器。 我要切换到的控制器NewRightController是一个UIViewController子类 – 在故事板中,我将控制器的大小设置为“Freeform”,并更改视图的大小以匹配embedded式控制器视图的大小。 这实际上并不影响视图的大小(它仍然以全屏方式logging),这只是使布局子视图更容易。

-(IBAction)switchToNewRight:(id)sender { UIView *rcView = [(ViewController *)self.parentViewController rightView]; // rcView is the right container view in my root view controller that self is embedded in. NewRightController *newRight = [self.storyboard instantiateViewControllerWithIdentifier:@"NewRight"]; newRight.oldRightController = self; // pass self to new controller so I can come back to the same instance newRight.view.frame = rcView.bounds; [self.parentViewController addChildViewController:newRight]; [self moveToNewController:newRight]; } -(void)moveToNewController:(UIViewController *) newController { UIView *rcView = [(ViewController *)self.parentViewController rightView]; [self willMoveToParentViewController:nil]; [self.parentViewController transitionFromViewController:self toViewController:newController duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{} completion:^(BOOL finished) { [self removeFromParentViewController]; [rcView constrainViewEqual:newController.view]; [newController didMoveToParentViewController:self]; }]; } 

经过多次实验后,我发现获取视图大小正确并在旋转后正确resize的方法是,将新控制器视图的框架初始设置为容器视图的边界,但在过渡animation之后,将约束添加到在旋转之后保持它的大小的新视图。 由于这个代码可能会反复使用,所以我把它放在UIView的一个类别上,用一个方法constrainViewEqual :. 这是这个代码:

 -(void)constrainViewEqual:(UIView *) view { [view setTranslatesAutoresizingMaskIntoConstraints:NO]; NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]; NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]; NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:0 toItem:view attribute:NSLayoutAttributeWidth multiplier:1 constant:0]; NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:0 toItem:view attribute:NSLayoutAttributeHeight multiplier:1 constant:0]; NSArray *constraints = @[con1,con2,con3,con4]; [self addConstraints:constraints]; } 

所以在玩了很多游戏后,我就开始工作了,就像我设想的那样。 目前没有任何转换,但我想发布代码供其他人查看。

在故事板中,我有一个整个游戏的视图控制器,它有一个视图容器和一个button。 embedded到游戏视图中的视图是包含不同types问题的页面控制器。 然后在故事板中未附加的是不同types的问题布局,在我的情况下是teamDisplay和locationDisplay。

在QuestionViewController中,我向.h文件添加了两个属性:

 @property (nonatomic, strong) UIPageViewController *pageViewController; @property (nonatomic, strong) NSMutableArray *questionTypeViewControllers; 

宣布一种方法:

 -(void)changeView; 

在.m文件中,合成它们:

 @synthesize questionTypeViewControllers, pageViewController; 

在viewDidLoad方法中:

 pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; UIViewController *ldvc = [self.storyboard instantiateViewControllerWithIdentifier:@"locationDisplay"]; UIViewController *tdvc = [self.storyboard instantiateViewControllerWithIdentifier:@"teamDisplay"]; questionTypeViewControllers = [NSMutableArray arrayWithObjects:ldvc, tdvc, nil]; NSArray *initView = [NSArray arrayWithObject:ldvc]; [pageViewController setViewControllers:initView direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; [self addChildViewController:pageViewController]; [self.view addSubview:self.pageViewController.view]; 

然后执行changeView方法:

 NSArray *initView = [NSArray arrayWithObject: [questionTypeViewControllers objectAtIndex:1]]; [pageViewController setViewControllers:initView direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 

在button所在的GameViewController中,向button添加一个动作,并从QuestionViewController中调用新创build的changeView方法。

 - (IBAction)change:(id)sender { // Need a more reliable way to get the QuestionViewController. [[self.childViewControllers objectAtIndex:0] changeView]; } 

就是这样。 一个没有脑子的权利?! ;)

我不知道这是否是正确的方式来做到这一点,但它的工作。 任何build议或改进是值得欢迎的。

我想做同样的事情,这就是我发现的。

创build自定义容器视图控制器

它的要点是你创build一个导航控制器,你embedded到容器视图,它允许你浏览多个视图,如你所愿。

这是一个简短的例子/演练:

  1. 将导航控制器添加到您的故事板。
  2. 将导航控制器embedded到容器视图中。
  3. select一个视图作为导航控制器的根视图控制器。
  4. 为容器视图内的每个视图控制器创build手动循环, 包括根视图控制器 (我几乎犯了不加这个的错误)。 确保你给每个你创build的segue命名。
  5. 在您的代码中,只要您想显示不同的视图控制器,只需调用相应的手动search。

希望这可以帮助!