【问题标题】:How to dismiss multiple view controllers which have been presented not pushed?如何关闭已呈现未推送的多个视图控制器?
【发布时间】:2017-02-08 05:12:02
【问题描述】:

场景:

我需要在每个弹出窗口中单击按钮一个接一个地显示 3 个或更多弹出窗口。我为每个弹出窗口创建了不同的 viewcontrollerxib 文件。因此,为了显示每个弹出窗口,我使用了 presentViewController 而不是 pushViewController

也就是说,我用过这个:

[self presentPopupViewController:searchPopUpView animationType:0];

而不是

[self.navigationController pushViewController:searchPopUpView animated:YES];

为了消除弹出窗口,编写了以下代码:

[self dismissPopupViewControllerWithanimationType:0];

问题:

弹出窗口显示完美,但每当弹出窗口出现时,背景就会变得越来越暗。在所有弹出窗口都被关闭后,我最终必须单击空白屏幕以删除那些较暗的部分。如何克服这个问题?

【问题讨论】:

  • 你能添加 UI 图片吗?
  • [self presentViewController:yourobj 动画:YES 完成:nil];并解雇 [self dismissViewControllerAnimated:YES completion:nil];
  • 朋友,我想它会帮助你。
  • 检查你的[self.childViewControllers count]然后逐个运行for循环取出视图控制器并将其从parentViewController中删除,这个技巧会帮助你。
  • @Lalitkumar 它不是那样工作的。如果我添加了这些代码,那么它将显示为完整的 vie 控制器而不是弹出窗口

标签: ios objective-c popup viewcontroller


【解决方案1】:

我认为您正在使用 MJPopupViewController 来显示弹出窗口。

如果是这样,那就试试这个。

假设有一个 controllerA,您希望从中显示一个弹出式控制器 popupControllerB

然后在你的 controllerA 中添加 Notifications Observer

controllerA 中编写的代码:

// Add Notification Observer when your view initialise.
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dismissPopup) name:@"DISMISS_POPUP" object:nil];

viewWillDisappear中移除通知观察者

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

当您从 popupControllerB

发布通知时,将调用此方法
-(void)dismissPopup {
[self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
}

而在popupControllerB中,在你想关闭Pop-up的地方,写下这段代码。

[[NSNotificationCenter defaultCenter] postNotificationName:@"DISMISS_POPUP" object:nil];

以上代码行将调用您的 controllerA 中编写的方法并正确关闭弹出窗口。

【讨论】:

    【解决方案2】:

    如果您想关闭呈现的UIViewControllers,您可以使用此代码。我已经使用这种方法来解雇presentedViewControllers。它会在您的rootViewController 上关闭您所有的presentedViewControllers

    UIViewController* presVC = self.window.rootViewController;
    
    while (presVC) {
        UIViewController* temp = vc.presentingViewController;
         if (!temp.presentedViewController) {
             [vc dismissViewControllerAnimated:NO completion:^{}];
             break;
           }
          vc =  temp;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 2019-03-28
      • 2011-01-15
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多