0

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.

Maxim Pontyushenko
  • 2,983
  • 2
  • 25
  • 36
AbhiRam
  • 2,033
  • 7
  • 41
  • 94
  • once you push from child 1 to child 2, you can pop child2, so child 1 will be shown!!Why do u want to push to child1 ? – Teja Nandamuri Nov 25 '15 at 13:49
  • Hi Mr.T i am using here animations see my above code using animations only in have to pesh back from Child2 to child1 – AbhiRam Nov 25 '15 at 14:57

1 Answers1

0

Inside the ChildViewController2 back button action you have:

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

Don't you mean:

ChildViewController1 *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController1"];
Brooks Hanes
  • 425
  • 2
  • 12
  • yes your right when i click back button i want push back from child2 to child1 if you know solution to this please help me – AbhiRam Nov 25 '15 at 14:58
  • So why are you adding the ViewController2 inside of its own method? Why aren't you adding ViewController1 instead? Seems we're getting 2 instances of the ViewController2 and just moving one of them away, exposing itself again...? – Brooks Hanes Nov 25 '15 at 14:59
  • yes your right but i am expecting solutions to this problem from you – AbhiRam Nov 26 '15 at 03:47