I am beginner in iOS. In my ParentViewController I am adding one ChildViewController. In that controller I have added one Next button. When I click that button I am pushing First ChildViewController to Second ChildViewController. That's fine.
But here I have added another Button in Second ChildViewController. When i click that button I want to push back from Second ChildViewController to First ChildViewController. But with my code that's not working. Please help me someone
My code:
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)];
}];
}
Here when I click this back button it's not pushing it back.