从子视图控制器转移到父视图控制器 - iOS

时间:2013-04-30 06:07:15

标签: ios objective-c parent-child parentviewcontroller uiview-hierarchy

我有一个主要的动作视图控制器,并且有按钮“查看”。查看Button的功能是:

 - (IBAction)btnReview:(id)sender
{
    ReviewViewController *vc = [[ReviewViewController alloc]initWithNibName:@"ReviewViewController" bundle:nil];
    [self addChildViewController:vc];
    [vc didMoveToParentViewController:self];
    [self.view addSubview:vc.view];
}

现在在评论页面上,我有一个按钮。在该按钮的动作上,我想切换回父视图控制器。应显示其视图。我尝试过以下代码,它会暂停或崩溃应用程序。

[self didMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];

我甚至尝试过:

[self dismissModalViewControllerAnimated:YES]

我也阅读过与此问题相关的其他帖子,但找不到满意的答案。请帮助!

7 个答案:

答案 0 :(得分:3)

我假设这是一个自定义的viewcontroller容器,你正在尝试构建," self"是你正在谈论的主要动作viewController。

试试这个:

- (IBAction)btnReview:(id)sender
{
    ReviewViewController *vc = [[ReviewViewController alloc]initWithNibName:@"ReviewViewController" bundle:nil];

    [self addChildViewController:vc];

    [self.view addSubview:vc.view];

    [vc didMoveToParentViewController:self];

    vc = nil;

}

对于" Back"按钮,我假设这是从Review viewController返回Main Actions viewController的Button,试试这个:

- (IBAction)btnReviewHide:(id)sender
 {

    [self willMoveToParentViewController:nil];
    [self.view removeFromSuperview];
    [self removeFromParentViewController];

 }

希望这有帮助。

答案 1 :(得分:2)

你应该使用委托。当您点击ReviewViewController中的按钮时,您会调用以下方法:[self.delegate hideReviewController:self];

这种方法看起来像:

- (void)hideReviewController:(ReviewViewController *)controller {
                [UIView animateWithDuration:0.3
                                  delay:0
                                options:UIViewAnimationOptionCurveEaseInOut
                             animations:^{
                                  controller.view.alpha = 0;
                             } completion:^(BOOL finished) {
                                  [self.view removeSubview:controller.view];
                                  // If you want the Review Controller to be deallocated:
                                  [self removeChildViewController:controller];
                             }];
}

修改 在ReviewDelegate.h文件中添加:

@class ReviewViewController;

@protocol ReviewDelegate <NSObject>
- (void)hideReviewController:(ReviewViewController *)controller;
@end

然后添加此属性:

@property (nonatomic, weak) id <ReviewDelegate> delegate;

在父类中:

 - (IBAction)btnReview:(id)sender
{
    ReviewViewController *vc = [[ReviewViewController alloc]initWithNibName:@"ReviewViewController" bundle:nil];
    vc.delegate = self;
    [self addChildViewController:vc];
    [vc didMoveToParentViewController:self];
    [self.view addSubview:vc.view];
}

    - (void)hideReviewController:(ReviewViewController *)controller {
                    [UIView animateWithDuration:0.3
                                      delay:0
                                    options:UIViewAnimationOptionCurveEaseInOut
                                 animations:^{
                                      controller.view.alpha = 0;
                                 } completion:^(BOOL finished) {
                                      [self.view removeSubview:controller.view];
                                      // If you want the Review Controller to be deallocated:
                                      [self removeChildViewController:controller];
                                 }];
    }

我还建议您阅读delegation

答案 2 :(得分:2)

试试这个

self.view=nil;
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];

答案 3 :(得分:1)

你不需要让它变得复杂......只需使用UINavigationController即可。

ParentVC导航到ChildVC

  ChildViewController *ChildVC = [[ChildViewController alloc]initWithNibName:@"ChildViewController" bundle:nil];
[self.navigationController pushViewController:ChildVC animated:YES];

ChildVC导航回ParentVC

ParentViewController *ParentVC = [[ParentViewController alloc]initWithNibName:@"ParentViewController" bundle:nil];

[self.navigationController popViewControllerAnimated:YES];

答案 4 :(得分:1)

由于您要将子视图作为子视图添加到父级,您需要从其超级视图(在您的情况下为父视图)中手动设置。而不是将其添加为子视图,您可以使用presentViewController方法,如下所示:

 - (IBAction)btnReview:(id)sender
  {

   ReviewViewController *vc = [[ReviewViewController alloc] initWithNibName:@"ReviewViewController" bundle:nil];
   [self presentViewController:vc
                      animated:YES
                    completion:nil];
  } 

并且在后退按钮的子类代码中将是:

  -(IBAction)backButtonClicked:(id)sender
  {
      [self dismissViewControllerAnimated:YES completion:nil];
  }

答案 5 :(得分:0)

听起来像你正在寻找的东西可以用UINavigationController来处理。只需使用pushViewController:将新视图推送到屏幕上即可。然后,视图顶部的导航栏中将出现一个后退按钮,可用于返回“父”视图控制器。

请参阅documentation

答案 6 :(得分:0)

仅写作

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

在我的案例中工作