views:

28

answers:

2

Hello all

Right I know how to do the pictue book effect but I struggling to remove the view

So page 1, I add the second page view via addSubview (via a swipe). I also increment a counter so I know what page I am on.

So how do I return to page 1? See I thought it would be self.view removeFromSuperview but that crashes when you try to go back to page 2

Code below

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
  //NSLog(@"swipe right to left");

  UniversalAppAppDelegate *appDelegate = (UniversalAppAppDelegate *)[[UIApplication sharedApplication] delegate];
  PictureBookViewController *viewController2 =(PictureBookViewController *) [appDelegate getViewControllerForViewID:@"81"];

  [UIView beginAnimations:Nil context:nil];
        [UIView setAnimationDuration:0.6];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view.superview cache:YES];
  self.viewController = viewController2;
  self.viewController.pageNo = self.pageNo + 1;

        [self.view addSubview:self.viewController.view];
        [UIView commitAnimations];
  [viewController2 release];

 } else {

  //NSLog(@"swipe left to right");
  NSLog([NSString stringWithFormat:@"%d", self.pageNo]);
  if (self.pageNo > 0) {

   [UIView beginAnimations:Nil context:nil];
   [UIView setAnimationDuration:0.6];
   [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES];

   //self.viewController = viewController2;
   //self.viewController.pageNo = self.pageNo - 1;
   //[self.view addSubview:self.viewController.view];

   [self.view removeFromSuperview];
   //[self.view addSubview:self.viewController.view];
   [UIView commitAnimations];

  }
    }
}

Update:

Each view has its own view controllers.

+1  A: 

You want to remove your viewController's view, not the container's view.

[self.viewController.view removeFromSuperview];
coob
Right I made your changes and now it keeps page 2 and removes page 1. So if page 1 had a pink background and page 2 had a red background, the pink background does not come back, when you swipe back, the red one just reappears
Burf2000
A: 

This seems to work and not crash? I am now convinced it right. I just seem to need a way of removing the last added subview. My first view is not the superview

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        //NSLog(@"swipe right to left");

        UniversalAppAppDelegate *appDelegate = (UniversalAppAppDelegate *)[[UIApplication sharedApplication] delegate];
        PictureBookViewController *viewController2 =(PictureBookViewController *) [appDelegate getViewControllerForViewID:@"82"];

        [UIView beginAnimations:Nil context:nil];
        [UIView setAnimationDuration:0.6];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view.superview cache:YES];
        self.viewController = viewController2;
        self.viewController.pageNo = self.pageNo + 1;
        self.viewController.lastViewController = self;
        [self.view addSubview:self.viewController.view];
        [UIView commitAnimations];
        [viewController2 release];

    } else {

        //NSLog(@"swipe left to right");
        NSLog([NSString stringWithFormat:@"%d", self.pageNo]);
        if (self.pageNo > 0) {

            [UIView beginAnimations:Nil context:nil];
            [UIView setAnimationDuration:0.6];
            [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES];
            self.pageNo --;
            self.view =  self.viewController.view;
            [self.view  removeFromSuperview ];
            self.view = self.lastViewController.view;

            [UIView commitAnimations];

        }
    }
}
Burf2000