从非UIViewController类(可可类的接口类)到会话vc获取值(我正在使用通知中心)后,我试图显示会话vc。
现在是第一次,我的会话vc在我当前的顶级vc上正确显示。但是第二次之后,我只得到白屏。但是会话vc中具有的api调用值仍在运行。我可以在控制台中看到。
这是我的代码:
TopView.swift:
import UIKit
class TopView:NSObject{
class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(base: selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
Appdelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.onOver(notification:)), name: NSNotification.Name(rawValue: "openBoard"), object: nil);
}
@objc func onOver(notification: NSNotification)
{
if let score = notification.object as? NSDictionary
{
DispatchQueue.background(delay: 0.1000, completion:{
DispatchQueue.main.async {
let storyBoard = UIStoryboard.init(name: "Trai", bundle: nil);
if let sessionvc = storyBoard.instantiateViewController(withIdentifier:
"sessionvc") as? sessionvc{
if let navController = TopView.topViewController()?.navigationController as? UINavigationController{
TopView.topViewController()?.navigationController?.pushViewController(sessionvc, animated: true)
}else{
TopView.topViewController()?.present(sessionvc, animated: true)
}
}
}
})
}
}
无论如何,我添加了断点。它总是会出现在下面这行:
TopView.topViewController()?.present(sessionvc, animated: true)
第一次,它工作正常,第二次只出现白屏。不确定是否需要从顶部或当前vc中删除任何vc。
任何解决方案都会有所帮助。
谢谢
已更新:
@objc func onOver(notification: NSNotification)
{
if let score = notification.object as? NSDictionary
{
let storyBoard = UIStoryboard.init(name: "Trai", bundle: nil);
if let sessionvc = storyBoard.instantiateViewController(withIdentifier:
"sessionvc") as? sessionvc{
if let navController = TopView.topViewController()?.navigationController as? UINavigationController{
TopView.topViewController()?.navigationController?.pushViewController(sessionvc, animated: true)
}else{
TopView.topViewController()?.present(sessionvc, animated: true)
}
}
}