Question

I have button on page 1 when i click on button i am performing some animation and navigation to new page say Page 2.

On page 2 there is cancel button when i click on that i want to go back to my previuos page i.e Page 1 but on click of cancel button my app crashes.

Here is my sample code on Page 1:Name of page CRViewController

- (IBAction)ClickMe:(id)sender {
    RegistraionViewController *secondController = [[RegistraionViewController alloc] init];

    CATransition *transitionAnimation = [CATransition animation];
    [transitionAnimation setDuration:1];
    [transitionAnimation setType:@"push"];
    [transitionAnimation setSubtype:kCATransitionFromBottom];
    [transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

    [self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFromBottom];
    [self.view addSubview:secondController.view];
}

Code which is on Page 2: Name of page RegistraionViewController (on Which crashes on click.)

- (IBAction)Click:(id)sender {
    CRViewController *secondController = [[CRViewController alloc] init];

    CATransition *transitionAnimation = [CATransition animation];
    [transitionAnimation setDuration:1];
    [transitionAnimation setType:@"push"];
    [transitionAnimation setSubtype:kCATransitionFromTop];
    [transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

    [self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFromBottom];
    [self.view addSubview:secondController.view];
}
Was it helpful?

Solution

Try this:

- (IBAction)ClickMe:(id)sender {   
    RegistraionViewController *secondController = [[RegistraionViewController alloc] init];   
    [self.navigationController pushViewController:secondController animated:YES];   
}


- (IBAction) Click:(id)sender {    
   [self.navigationController popViewControllerAnimated:YES];    
}

Edit:
I'm sorry. If you want custom some UIViewController transitions animation, see below:
UIViewController transition - objective-c
http://www.teehanlax.com/blog/custom-uiviewcontroller-transitions/
Or you can try this:

- (IBAction)ClickMe:(id)sender {
    RegistraionViewController *secondController = [[RegistraionViewController alloc] init];

    CATransition *transitionAnimation = [CATransition animation];
    [transitionAnimation setDuration:1];
    [transitionAnimation setType:@"push"];
    [transitionAnimation setSubtype:kCATransitionFromBottom];
    [transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

    [self.navigationController.view.layer addAnimation:transitionAnimation forKey:nil];
    [self.navigationController pushViewController:secondController animated:NO]; 
}

- (IBAction)Click:(id)sender {
    CRViewController *secondController = [[CRViewController alloc] init];

    CATransition *transitionAnimation = [CATransition animation];
    [transitionAnimation setDuration:1];
    [transitionAnimation setType:@"push"];
    [transitionAnimation setSubtype:kCATransitionFromTop];
    [transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

    [self.navigationController.view.layer addAnimation:transitionAnimation forKey:nil];
    [self.navigationController popToViewController:secondController animated:NO];
}

OTHER TIPS

Your problem is lying somewhere because of the subview. I have tried your code.

I did the following code in my project with some animation hope it helps you.

CATransition *animation = [CATransition animation];
    [animation setDelegate:self];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    [animation setDuration:0.8];
    [animation setType:kCATransitionFade];
    [animation setSubtype:kCATransitionMoveIn];

    //[[self.view layer]addAnimation:animation forKey:@"fview"];

    [self.navigationController.view.layer addAnimation:animation forKey:nil];

    [self.navigationController pushViewController:yourView animated:NO];

In animation type you can give whichever animation you want.

Hope that helps you.

*

- (IBAction)ClickMe:(id)sender {
    RegistraionViewController *secondController = [[RegistraionViewController alloc] init];
    CATransition *transitionAnimation = [CATransition animation];
    [transitionAnimation setDuration:1];
    [transitionAnimation setType:@"push"];
    [transitionAnimation setSubtype:kCATransitionFromBottom];
    [transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    [self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFromBottom];
    [self addChildViewController:secondController];
    [self.view addSubview:secondController.view];
}

* replace your sample code on Page 1: with the above and then check.

Hope this helps to you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top