在swift中使用DispatchQueue

时间:2018-04-19 07:16:24

标签: ios swift

我正在使用dispatcQueue自动关闭我的控制器,但我有一个问题是它会自动关闭我的视图控制器,但也会关闭其他模态显示的视图控制器,我想只关闭一个视图控制器

func dismissViewController() {
    let pickVc = UIStoryboard(name:"Main", bundle: nil).instantiateViewController(withIdentifier: "request-VC") as! RequestViewController
    pickVc.modalPresentationStyle = .custom
    pickVc.requestDelegate = self
    present(pickVc, animated: true, completion: nil)
}

func openRequestAlertViewController() {
    let pickVc = UIStoryboard(name:"Main", bundle: nil).instantiateViewController(withIdentifier: "requestAlertVC") as! RequestAlertViewController
    pickVc.modalPresentationStyle = .custom
    pickVc.searchDelegate = self
    present(pickVc, animated: true, completion: nil)
    DispatchQueue.main.asyncAfter(deadline: .now() + 20) {
        self.dismiss(animated: true, completion: nil)
    }
}

func dismissRequestVC() {
    let searchVc = UIStoryboard(name:"Main", bundle: nil).instantiateViewController(withIdentifier: "searching-VC") as! SearchingViewController
    searchVc.modalPresentationStyle = .custom
    present(searchVc, animated: true, completion: nil)
}

但它也在20秒后解散,我不想解雇这个。任何帮助?这是我的整个代码

2 个答案:

答案 0 :(得分:1)

为您要解除的特定viewcontroller创建一个实例,如下所示

var lvc: LoadingViewController?

func showLoadingIn(viewController: UIViewController) {
   lvc = LoadingViewController() // create new instance before presentation
   viewController.presentViewController(lvc!, animated: false, completion: nil)
}

func dismissLoader() {
    lvc?.dismissViewControllerAnimated(true) {
        print("Dismissing Loader view Controller") 
    }
}

答案 1 :(得分:1)

您正在使用当前的viewController呈现所有viewControllers,而在解雇时您并没有忽略当前呈现的viewController。相反,你正在解雇presenter viewController,所以所有呈现的ViewControllers也被解雇了。

如果要关闭特定的viewController,可以这样做,

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

例如,在您的情况下,您可以执行此操作以在20秒后解除pickVc

DispatchQueue.main.asyncAfter(deadline: .now() + 20) {
    pickVc.dismiss(animated: true, completion: nil)
}