3

我有一个主要的 Actions View Controller,上面有一个“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]

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

4

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

 }

希望这可以帮助。

于 2013-08-18T12:44:44.560 回答
2

尝试这个

self.view=nil;
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
于 2014-01-16T06:18:19.683 回答
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];
                                 }];
    }

我还建议您阅读有关委派的内容

于 2013-04-30T06:16:36.033 回答
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];
  }
于 2013-04-30T07:03:10.287 回答
1

你不需要让它复杂......只需使用UINavigationController.

ParentVC到导航ChildVC

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

ChildVC对于从to导航回来ParentVC

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

[self.navigationController popViewControllerAnimated:YES];
于 2013-04-30T06:17:07.393 回答
0

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

请参阅文档

于 2013-04-30T06:13:03.450 回答
0

只写

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

在我的情况下工作

于 2016-05-20T11:38:59.870 回答