在层次结构中解除UINavigationController中所有呈现的ViewControlelrs

时间:2017-11-07 07:06:57

标签: objective-c swift swift3 uinavigationcontroller presentviewcontroller

我正在寻找一种方法来解析gluLookAt()层次结构中所有模态呈现的viewControllers而不知道它们的名称。所以我结束了while循环,如下所示:

夫特

UINavigationController

目标c

while(navigationController.topViewController != navigationController.presentedViewController) {
      navigationController.presentedViewController?.dismiss(animated: true, completion: nil)
}

我想逐个解雇所有while(![self.navigationController.topViewController isEqual:self.navigationController.presentedViewController]) { [self.navigationController.presentedViewController dismissViewControllerAnimated:YES completion:nil]; } ,直到presentedControllerspresentedViewController变得相等。

问题是即使在解雇后topViewcontroller也没有改变。

即使在解雇后它仍然保持不变,我最终得到navVC.presentedViewController

有谁知道问题出在哪里?

7 个答案:

答案 0 :(得分:0)

形成您的问题我理解您要关闭根视图控制器上方的所有视图控制器。为此你可以这样做:

self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)

答案 1 :(得分:0)

不需要使用self.navigationController.presentedViewController。

可能会有所帮助!我的代码如下: 的目标c

[self dismissViewControllerAnimated:YES completion:^{

}];
// Or using this 

dispatch_async(dispatch_get_main_queue(), ^{
    [self dismissViewControllerAnimated:YES completion:nil];
});

答案 2 :(得分:0)

请检查此代码

-(void)dismissModalStack {
UIViewController *vc = self.window.rootViewController;
while (vc.presentedViewController) {
    vc = vc.presentedViewController;
    [vc dismissViewControllerAnimated:false completion:nil];
}

}

答案 3 :(得分:0)

我找到了答案。我可以通过以下方式解除navigationController上的所有presentedViewControllers

navigationController.dismiss(animated: true, completion: nil)

保留topViewController并关闭所有其他模态。

答案 4 :(得分:0)

很高兴看到你找到了答案,我已经通过另一种方式做到了这一点。

您可以创建一个BaseViewController(实际上有很多应用程序都可以这样做),并定义了一个属性,例如“呈现控件”。在appdelegate中指示呈现的ViewController,然后在viewWillAppear方法中设置属性,使其始终指示顶视图控制器。

-(void)viewWillAppear:(BOOL)animated{  

    AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];  
    delegate.presentingController = self;    
}

从BaseViewController继承的所有类都将调用它。如果要关闭所有控制器,只需循环如下:

- (void)clickButton:(id)sender {  

    AppDelegate *delegate=(AppDelegate *)[[UIApplicationsharedApplication]delegate];  

    if (delegate.presentingController)  
    {  
        UIViewController *vc =self.presentingViewController;  

        if ( !vc.presentingViewController )   return;  

        while (vc.presentingViewController)  
        {  
            vc = vc.presentingViewController;  
        }  

        [vc dismissViewControllerAnimated:YEScompletion:^{  

        }];  
    }  
}

希望这会对你有所帮助:)。

答案 5 :(得分:0)

在我的情况下,除了:

func dismissToSelf(completion: (() -> Void)?) {
    // Collecting presented
    var presentedVCs: [UIViewController] = []
    var vc: UIViewController? = presentedViewController
    while vc != nil {
        presentedVCs.append(vc!)
        vc = vc?.presentedViewController
    }

    // Dismissing all but first
    while presentedVCs.count > 1 {
        presentedVCs.last?.dismiss(animated: false, completion: nil)
        presentedVCs.removeLast()
    }

    // Dismissing first with animation and completion
    presentedVCs.first?.dismiss(animated: true, completion: completion)
}

答案 6 :(得分:0)

当新的推送通知到达时,我有一个类似的问题,即删除/关闭现有/以前的推送通知,其中不同的图片作为推送通知发送。 在我的情况下,使用 Swift 5,我想删除/关闭之前的推送通知并单独显示一个新的推送通知,无论用户是否确认之前的通知(即没有用户确认)。

我尝试了 Kadian 的建议,并进行了微小的更改,结果完美无缺。

这是我的 NotificationDelegate.swift

import UIKit
import UserNotifications

extension AppDelegate: UNUserNotificationCenterDelegate {
  func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    willPresent notification: UNNotification,
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    completionHandler([.alert, .sound, .badge])   
  }

  func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    didReceive response: UNNotificationResponse,
    withCompletionHandler completionHandler: @escaping () -> Void) {
    
    UNUserNotificationCenter.current().removeAllDeliveredNotifications()

    defer { completionHandler() }
    guard response.actionIdentifier == UNNotificationDefaultActionIdentifier else {return}

    let payload = response.notification.request.content
    
    let pn = payload.body  
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
    let vc = storyboard.instantiateViewController(withIdentifier: pn)

    //***Below cmd will erase previous push alert***
    self.window!.rootViewController?.dismiss(animated: false, completion: nil)

    //Below cmd will display a newly received push notification
    self.window!.rootViewController!.present(vc, animated: false)

  }
  }