iphone:像图画书一样切换视图,removeFromSuperView有问题

时间:2010-09-23 11:21:34

标签: iphone views switching

对,我知道怎么做图画书效果,但我努力删除视图

所以第1页,我通过addSubview添加第二页视图(通过滑动)。我也增加了一个计数器,所以我知道我在哪个页面。

那我该怎么回到第1页呢?看到我以为它会是self.view removeFromSuperview但是当你试图回到第2页时崩溃

以下代码

- (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];

  }
    }
}

更新

每个视图都有自己的视图控制器。

2 个答案:

答案 0 :(得分:1)

您想要删除viewController的视图,而不是容器的视图。

[self.viewController.view removeFromSuperview];

答案 1 :(得分:0)

这似乎工作而不是崩溃?我现在确信它是正确的。我似乎需要一种方法来删除最后添加的子视图。我的第一个观点不是超级视图

- (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];

        }
    }
}