从子视图控制器切换到父视图控制器 - iOS

3

我有一个主要的操作视图控制器,上面有一个名为“Review”的按钮。Review按钮的功能是:

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

我也阅读了其他相关帖子,但没有找到令人满意的答案。请帮忙!


不确定这是否是问题所在,但您不应该使用nil参数调用didMoveToParentViewController:removeFromParentViewController在视图控制器被移除后应自动调用它。 - omz
即使我删除 [self didMoveToParentViewController:nil];,它仍会暂停应用程序并最终崩溃。 - user2334616
7个回答

3
我猜想您正在尝试构建一个自定义的视图控制器容器,“self”是您所讨论的主要操作视图控制器。请尝试以下方法:
- (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;

}

针对“返回”按钮,我认为这是从Review viewController返回到Main Actions viewController的按钮,请尝试以下操作:

- (IBAction)btnReviewHide:(id)sender
 {

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

 }

希望这可以帮助到您。

2

试试这个

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

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

我建议您也阅读有关委托的内容。


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

从ParentVC导航到ChildVC正常工作,但从ChildVC返回到ParentVC时崩溃。 - user2334616
你是否已经实现了从父视图控制器到子视图控制器的导航中的'pushViewController:'方法? - Vineet Singh
你在父视图控制器的接口文件和子视图控制器中都实现了 UINavigationControllerDelegate 吗? - Vineet Singh

1

由于您将子视图添加为父视图的子视图,因此您需要手动从其父视图(在您的情况下是parentview)中删除它。而不是将其添加为子视图,您可以使用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];
  }

0
听起来你需要的可以通过使用UINavigationController来处理。只需使用pushViewController:将新视图推送到屏幕上。然后,一个返回按钮将出现在导航栏顶部的视图中,可用于返回到“父”视图控制器。
请参阅文档

不,我没有使用UINavigationController,因为我的应用程序是基于选项卡的。 - user2334616
请参考上面Levi的回答。 - The Kraken

0

只写代码

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

在我的情况下有效


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接