我正在编写一个函数,当我的应用程序在本地收到远程通知时将调用该函数。首先,使用库BRYXBanner显示横幅,然后当用户点击横幅时,它将调用此函数,该函数应呈现名为ChatLogViewController的视图控制器。可以从app中的任何位置调用此函数,因此该函数的一个参数是fromViewController。我想弄清楚两个问题。
代码:
func GoToClassVC(fromVC : UIViewController, toClassID: String) {
let chatLog = ChatLogViewController()
fromVC.present(chatLog, animated: true) {
chatLog.classID = toClassID
}
}
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)
感谢您的帮助!
答案 0 :(得分:1)
要解决此错误无法将类型'()'的值指定为'(() - >())?'
替换此代码
banner.didTapBlock = GoToClassVC(fromVC: currentVC, toClassID: self.backgroundLaunchClassID)
到这个
banner.didTapBlock = {
self.GoToClassVC(fromVC: UIApplication.shared.keyWindow!.visibleViewController()!, toClassID:"string")
}
For This:如何使用函数
在应用内的任何位置展示ChatLogViewController
GoToChatLogVC()
您的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;
}
}
}
}
答案 1 :(得分:0)
如果您想从任何地方展示ViewController,那么您可以在let status_height = UIApplication.sharedApplication().statusBarFrame.size.width
的{{1}}上展示它,如下所示。
rootViewController
答案 2 :(得分:0)
要从app delegate呈现视图控制器,您可以使用以下内容:
(if(a.getAttribute('data-type') == 'Recommended'){})
答案 3 :(得分:-1)
最好的方法是:
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)