繁体   English   中英

从AppDelegate呈现ViewController

[英]Presenting a ViewController From AppDelegate

我正在编写一个函数,当我的应用程序在本地接收到远程通知时,该函数将被调用。 首先,使用库BRYXBanner显示横幅,然后,当用户点击横幅时,它将调用此函数,该函数应提供一个称为ChatLogViewController的视图控制器。 可以从应用程序中的任何位置调用此函数,因此该函数的参数之一是fromViewController。 我试图找出两个问题。

  1. 如何使用函数GoToChatLogVC()从应用程序中的任何位置呈现ChatLogViewController

码:

func GoToClassVC(fromVC : UIViewController, toClassID: String) {
    let chatLog = ChatLogViewController()
    fromVC.present(chatLog, animated: true) {
        chatLog.classID = toClassID
    }
}
  1. 如何将此函数分配给(()->())类型的属性?

功能代码截图

guard let currentVC = self.window?.currentViewController() else {
    print("ERROR")
    return
}

let banner = Banner(title: title, subtitle: body, image: #imageLiteral(resourceName: "MessageIcon"), backgroundColor: UIColor(red:40.00/255.0, green:170.0/255.0, blue:226/255.0, alpha:1.000))

banner.show(duration: 3.0)

banner.didTapBlock = GoToClassVC(fromVC: currentVC, toClassID: self.backgroundLaunchClassID)

谢谢您的帮助!

解决此错误无法将类型'()'的值分配给类型'(()->())吗?'

替换此代码

banner.didTapBlock = GoToClassVC(fromVC: currentVC, toClassID: self.backgroundLaunchClassID)

对此

banner.didTapBlock = {
             self.GoToClassVC(fromVC: UIApplication.shared.keyWindow!.visibleViewController()!, toClassID:"string")
        }

为此:如何使用函数GoToChatLogVC()从应用程序中的任何位置呈现ChatLogViewController

currentVC更换到UIApplication.shared.keyWindow!.visibleViewController()!

extension UIWindow {

    func visibleViewController() -> UIViewController? {
        if let rootViewController: UIViewController = self.rootViewController {
            return UIWindow.getVisibleViewControllerFrom(vc: rootViewController)
        }
        return nil
    }

    class func getVisibleViewControllerFrom(vc:UIViewController) -> UIViewController {

        if vc.isKind(of:UINavigationController.self) {

            let navigationController = vc as! UINavigationController
            return UIWindow.getVisibleViewControllerFrom( vc: navigationController.viewControllers.first!)

        } else if vc.isKind(of:UITabBarController.self) {

            let tabBarController = vc as! UITabBarController
            return UIWindow.getVisibleViewControllerFrom(vc: tabBarController.selectedViewController!)

        }else if vc.isKind(of:UIAlertController.self) {

            let tabBarController = vc as! UIAlertController
            return UIWindow.getVisibleViewControllerFrom(vc: tabBarController)

        } else {

            if let presentedViewController = vc.presentedViewController {
                if (presentedViewController.presentedViewController != nil){
                    return UIWindow.getVisibleViewControllerFrom(vc: presentedViewController.presentedViewController!)
                }else{
                    return UIViewController()
                }

            } else {

                return vc;
            }
        }
    }
}

如果要从任何地方显示ViewController,则可以将其显示在UIWindow rootViewController ,如下所示。

if let rootVC = UIApplication.shared.keyWindow?.rootViewController {
    let chatLog = ChatLogViewController()
    rootVC.present(chatLog, animated: true, completion: nil)
}

要通过应用程序委托显示视图控制器,可以使用以下命令:

let chatLog = ChatLogViewController()
window().rootViewController?.present(chatLog, animated: true) { _ in }

最好的方法是这样的:

1-将此扩展名添加到您的AppDelegate类的顶部:

extension UIApplication{
    var topViewController: UIViewController?{
        if keyWindow?.rootViewController == nil{
            return keyWindow?.rootViewController
        }

        var pointedViewController = keyWindow?.rootViewController

        while  pointedViewController?.presentedViewController != nil {
            switch pointedViewController?.presentedViewController {
            case let navagationController as UINavigationController:
                pointedViewController = navagationController.viewControllers.last
            case let tabBarController as UITabBarController:
                pointedViewController = tabBarController.selectedViewController
            default:
                pointedViewController = pointedViewController?.presentedViewController
            }
        }
        return pointedViewController

    }
}

2-现在您可以轻松呈现视图控制器:

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "net")
application.topViewController?.present(vc, animated: true, completion: nil)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM