更改子视图控制器 [英] Change child view controller

查看:131
本文介绍了更改子视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图控制器,当我按下一个按钮时,出现一个子视图控制器。这工作完美,但我想改变这个子视图控制器为其他人,如果我按下按钮下一个是这里做做两步登录。
任何想法?因为从主视图控制器我知道如何显示一个孩子,但从孩子我不知道该怎么做。

I have a view controller that when I press a button a child view controller appear. This works perfectly but I want to change this child view controller for other one if I press the button next that is inside of this to do like two step login. Any idea? Because from the main view controller I know how show a child but from the child I don't know how to do it.

推荐答案

如果使用故事板,您可以创建自己的子代替segue,例如:

If using storyboards, you can create your own child replace segue, e.g.:

ReplaceSegue.h

ReplaceSegue.h

@interface ReplaceSegue : UIStoryboardSegue

@end

ReplaceSegue.m

ReplaceSegue.m

@implementation ReplaceSegue

- (void)perform
{
    UIViewController *source = self.sourceViewController;
    UIViewController *destination = self.destinationViewController;
    UIViewController *container = source.parentViewController;

    [container addChildViewController:destination];
    destination.view.frame = source.view.frame;
    [source willMoveToParentViewController:nil];

    [container transitionFromViewController:source
                           toViewController:destination
                                   duration:0.5
                                    options:UIViewAnimationOptionTransitionCrossDissolve
                                 animations:^{
                                 }
                                 completion:^(BOOL finished) {
                                     [source removeFromParentViewController];
                                     [destination didMoveToParentViewController:container];
                                 }];
}

@end

,在容器视图控制器和子控制器上放置容器视图,将自定义segue从第一个子场景放到第二个子场景。您需要为两个子场景之间的自定义segue指定 ReplaceSegue 类:

You can then open your storyboard, put a container view on your container view controller and on the child controller, put a custom segue from the first child scene to the second child scene. You'll want to specify the ReplaceSegue class for the custom segue between the two child scenes:

请注意,如果您已正确使用autolayout或自动调整第二个子视图,一切都会正常工作(特别是在 ReplaceSegue 中目标控制器视图的框架的手动设置)。但是在Interface Builder中,第二个子场景的布局看起来不太对,并且可以使场景的正确设计有点尴尬,因为(至少在Xcode 4.5中)第二个子级的模拟指标场景不能很好地推断出正确的大小。因此,您应该将第二个子级场景的模拟指标大小从Inferred更改为Freeform,然后手动调整第二个子级场景的大小:

Note that if you've properly used autolayout or autosizing on the second child view, everything will work fine (especially with the manual setting of the frame of the destination controller's view in the ReplaceSegue). But in Interface Builder, the layout of that second child scene doesn't look quite right and can make the proper design of the scene a little awkward, because (as of Xcode 4.5, at least) the "Simulated Metrics" for the second child scene doesn't do a good job inferring the the proper size. So you should probably change that second child scene's simulated metrics' size from "Inferred" to "Freeform" and then manually adjust the size of the second child scene:

不是非常优雅的Xcode体验,但它工作。如果您正确使用自动布局或自动调整大小的蒙版,则会非常优雅地处理微小的变化。

It's not very elegant Xcode experience, but it works. And if you're properly using autolayout or autosizing masks, minor variations are handled very gracefully.

这篇关于更改子视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆