1

在我的项目中,我使用 rootViewController?.dismiss 将一些视图控制器解散回主 VC。

在完成处理程序中,一旦 UIAlertController 再次可见,我想在主视图控制器上显示它。

我的问题是 UIAlertController 永远不会出现。我的预感是因为我在 launchFrom 参数中使用了 self 。我是否需要获得对主要 VC 的引用,并在那里展示它?

关闭 VC 并尝试显示警报:

self.view.window!.rootViewController?.dismiss(animated: false, completion: {
    Constants.presentThankYou(launchFrom: self)
})

presentThankYou 方法

static func presentThankYou(launchFrom: UIViewController) {
    let alertController = UIAlertController(title: "Thank You!", message: "You did it!", preferredStyle: UIAlertControllerStyle.alert)
    let okAction = UIAlertAction(title: "Close", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
        print("Done")
    }
    alertController.addAction(okAction)
    launchFrom.present(alertController, animated: true, completion: nil)
}

在我的所有 VC 都被解雇后,我如何提交警报控制器?

4

4 回答 4

3

在我的脑海中,尝试在viewDidAppear. 要防止警报一直显示,请使用 bool并在处理程序shouldShowThankYouAlert中将此值设置为 truedismiss completion

于 2017-12-18T23:39:14.770 回答
1

需要的话也可以参考这个,只要调用alert就可以在被解除的Controller的viewWillDisappear中显示

override func viewWillDisappear(_ animated: Bool) {
        if self.isMovingFromParentViewController {
            DispatchQueue.main.async {
                wrapperClass.BasicAlert("View is Dismissed", message: "", view: self)
            }
        }
    }

这将在视图完全关闭时显示警报

于 2017-12-19T05:10:41.853 回答
0

我没有检查,但是如何使用窗口而不是 UIViewController。

self.view.window!.rootViewController?.dismiss(animated: false, completion: {
   Constants.presentThankYou()
})

static func presentThankYou() {
   let alertController = UIAlertController(title: "Thank You!", message: "You did it!", preferredStyle: UIAlertControllerStyle.alert)
   let okAction = UIAlertAction(title: "Close", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
       print("Done")
    }
   alertController.addAction(okAction)
   appDelegate.window.present(alertController, animated: true, completion: nil)
}
于 2017-12-19T05:21:13.613 回答
-1

所以一个问题,完成块不需要主线程。我建议强制完成执行一些代码。

var topController: UIViewController? {
  if var temp = UIApplication.shared.keyWindow?.rootViewController {
    while let presentedViewController = temp.presentedViewController {
      temp = presentedViewController
    }
    return temp
  }
  return nil
}

self.view.window!.rootViewController?.dismiss(animated: false, completion: {
  DispatchQueue.main.async {
    guard let top = topController else { return }
    Constants.presentThankYou(launchFrom: top)
  }
})
于 2017-12-19T00:09:11.367 回答