如何使用动画将一个ChildViewController推送到另一个ChildViewController

时间:2015-11-25 13:43:34

标签: ios objective-c childviewcontroller

我是iOS的初学者。在我的ParentViewController中,我添加了一个ChildViewController。在那个控制器中,我添加了一个Next按钮。当我单击该按钮时,我将First ChildViewController推送到Second ChildViewController。没关系。

但是我在第二个ChildViewController中添加了另一个Button。当我点击该按钮时,我想从第二个ChildViewController推回到第一个ChildViewController。但是我的代码无效。请帮帮我一个人

我的代码:

ParentViewController:

#import "ParentViewController.h"

@interface ParentViewController ()

@end

@implementation ParentViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    ChildViewController1 *ViewController1 = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController1"];

    ViewController1.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    ViewController1.view.backgroundColor = [UIColor cyanColor];
    [ViewController1 willMoveToParentViewController:self];
    [self.view ViewController1.view];
    [self ViewController1];
}
@end

ChildViewController1:

#import "ChildViewController1.h"

@interface ChildViewController1 ()
{

}

@end

@implementation ChildViewController1

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)next:(id)sender{

    ChildViewController2 *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController2"];

    middleVC.view.hidden=FALSE;
    [middleVC.view setFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];

    [self addChildViewController:middleVC];
    [self.view addSubview:middleVC.view];
    [middleVC didMoveToParentViewController:self];

    [UIView animateWithDuration:0.5 animations:^{
        [middleVC.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    }];
}

ChildViewController2:

#import "ChildViewController2.h"

@interface ChildViewController2 ()
{

}

@end

@implementation ChildViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)back:(id)sender {

    ChildViewController2 *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController2"];

    middleVC.view.hidden=FALSE;

    [middleVC.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    [self addChildViewController:middleVC];
    [self.view addSubview:middleVC.view];
    [middleVC didMoveToParentViewController:self];

    [UIView animateWithDuration:0.5 animations:^{
        [middleVC.view setFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
    }];
}

当我点击此后退按钮时,它不会将其推回。

1 个答案:

答案 0 :(得分:0)

在ChildViewController2后退按钮操作中,您有:

 ChildViewController2 *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController2"];

不是吗?

ChildViewController1 *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController1"];