【问题标题】:How to dismiss current view controller from app delegate in swift?如何快速从应用程序委托中关闭当前视图控制器?
【发布时间】:2021-05-30 23:23:10
【问题描述】:

我想在某些情况下从 appDelegate 弹出当前视图控制器,但我不知道该怎么做,如果有任何想法请帮助我...... ..................................................... ......

import UIKit
import IQKeyboardManagerSwift

let kSharedAppDelegate        = UIApplication.shared.delegate as? AppDelegate
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        self.window = UIWindow(frame: UIScreen.main.bounds)
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
        IQKeyboardManager.shared.enableAutoToolbar = false
        //IQKeyboardManager.shared.toolbarTintColor = .white
        //IQKeyboardManager.shared.toolbarBarTintColor = ColorSet.appTheamColor
        kSharedAppDelegate?.moveToSplashVC()
        return true
    }
    

    
    func applicationDidBecomeActive(_ application: UIApplication) {
               // Check we can access the application window
               guard let window = UIApplication.shared.windows.first else {
                   return
               }
               // Check we can access the root viewController
               guard let vc = window.rootViewController else {
                   return
               }
               // Check the root vc is the type that we want to dismiss
               if vc is NoInternetPopUpViewController {
                   vc.dismiss(animated: true, completion: nil)
               }
    }

    
    //MARK:- Show No Internet connection VC
    func showNoInterNetVC() {
        guard  let controller = UIStoryboard(name: Storyboards.kNoInternet, bundle: nil).instantiateViewController(withIdentifier: Identifiers.kNoInternetPopUpViewController) as? NoInternetPopUpViewController else {return}
        
        controller.modalPresentationStyle = .overFullScreen
        controller.modalTransitionStyle = .crossDissolve
        kSharedAppDelegate?.window?.rootViewController?.present(controller, animated: true, completion: nil)
        //window.present(controller , animated: true)
    }
    
    
}

【问题讨论】:

  • 正如 Wez 在他的回答中所说,“pop”是 push 的反义词,用于导航控制器。在您的代码中,您可以调用dismiss(animated:completion:),它用于模态。这可能就是你想要的。

标签: ios swift uinavigationcontroller


【解决方案1】:

我认为pop 是错误的术语,除非您使用的是导航控制器。

如果你想dismiss当前呈现的viewController,你可以像这样检查应用程序WindowrootViewController

// Check we can access the application window
guard let window = UIApplication.shared.windows.first else {
    return
}
// Check we can access the root viewController
guard let vc = window.rootViewController else {
    return
}
// Check the root vc is the type that we want to dismiss
if vc is NoInternetPopUpViewController {
    vc.dismiss(animated: true, completion: nil)
}

我还刚刚注意到,您可能不需要通过 shared 属性访问应用程序单例,因为 applicationDidBecomeActive(_ application: UIApplication) 已经将 Application 传递给您 - 该行将变为:

guard let window = application.windows.first else {

【讨论】:

  • 这是一个很好的、彻底的答案。它会在关闭它之前检查以确保顶视图控制器是NoInternetPopUpViewController 的实例,这是一个好主意。 (已投票)
  • @AJ 让我们知道什么不起作用,您是否遇到任何错误?
  • 我得到了解决方案,但是如何匹配当前的视图控制器
  • var rootViewController = UIApplication.shared.keyWindow?.rootViewController if (window?.rootViewController?.isKind(of: NoInternetPopUpViewController.self))! { rootViewController?.dismiss(animated: true, completion: nil) }
【解决方案2】:

还有另一种方法可以做到这一点,在较新的 iOS 版本中,您可以直接访问 topViewController(),因此您可以像 UIApplication.topViewController() 一样访问它,唯一的缺点是您需要将其包装到 if let 语句中进行检查如果它不为空。仅供参考,如果您让didFinishLaunching() 方法在您的应用程序委托中至少运行一次,则在大多数情况下它不会为空。这样它就可以将视图控制器堆叠为顶视图控制器。这对您来说不是问题,因为如果出现这种情况,所有其他方法也会失败。

现在要做一个弹出视图控制器,您只需使用顶视图控制器并在其导航视图控制器上执行弹出,或者您可以在没有导航视图控制器的情况下将其关闭。

【讨论】:

  • UIApplication.topViewController() 记录在哪里?我在 Apple 的文档中找不到对它的引用。 (顺便说一句,这些库是 iOS 的一部分,而不是 Swift 语言。您所说的“...较新的 swift 版本您可以直接访问 topViewController()...”不太正确。iOS 的版本和版本Swift 是不同的东西。)
  • @DuncanC 我为 swift|iOS 问题道歉,我应该说清楚。另外,这里是developer.apple.com/documentation/uikit/uinavigationcontroller/…,是文档的链接。
猜你喜欢
  • 2013-10-29
  • 2012-08-09
  • 2014-09-09
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多