【问题标题】:Getting white screen second time after present the vc on top of current vc. Not getting it for first time在当前 vc 之上显示 vc 后第二次出现白屏。第一次没收到
【发布时间】:2019-10-24 13:00:28
【问题描述】:

在我从我的非 UIViewController 类(我的接口类,它来自我的可可类)获取值(我正在使用通知中心)到我的会话 vc 之后,我试图显示我的会话 vc。

现在,我的会话 vc 第一次在我当前的顶级 vc 上正确显示。但第二次之后,我只得到白屏。但是我的会话 vc 中的值、api 调用仍在运行。我可以在我的控制台中看到。

TopView.swift :

import UIKit

public extension UIWindow {
    public var visibleViewController: UIViewController? {
        return UIWindow.getVisibleViewControllerFrom(vc: self.rootViewController)
    }

    public static func getVisibleViewControllerFrom(vc: UIViewController?) -> UIViewController? {
        if let nc = vc as? UINavigationController {
            return UIWindow.getVisibleViewControllerFrom(vc: nc.visibleViewController)
        } else if let tc = vc as? UITabBarController {
            return UIWindow.getVisibleViewControllerFrom(vc: tc.selectedViewController)
        } else {
            if let pvc = vc?.presentedViewController {
                return UIWindow.getVisibleViewControllerFrom(vc: pvc)
            } else {
                return vc
            }
        }
    }
}

func getTopViewController() -> UIViewController? {
    let appDelegate = UIApplication.shared.delegate
    if let window = appDelegate!.window {
        return window?.visibleViewController
    }
    return nil
}

我的应用委托:

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 onGameOver(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 topVC = getTopViewController() {
                            //topVC.dismiss(animated: true, completion: nil)
                            topVC.present(sessionvc, animated: true)
                        }

                }

        }
    }

我第一次没有得到它。但是从第二次开始,如果我尝试做同样的事情,我会得到白屏,但我的 sessionvc 数据仍然会加载到我的控制台中。对此有任何帮助

【问题讨论】:

  • 代码正确。但是检查你的 topVC top vc 是否和第一次一样。如果您的 topVC 正在更改。那么您需要每次都调用您的 nonviewcontroller 类。将它分配给rootvc,然后继续。它会工作。
  • 是的,你能帮帮我吗

标签: ios swift iphone xcode uiviewcontroller


【解决方案1】:

可能是重播较晚。让我知道它是否对您有帮助。

改变你的方法@objc func onGameOver(notification: NSNotification)

if let topVC = getTopViewController() {

  let Yourvc = YourViewController()
  topVC = Yourvc
  topVC.present(topVC, animated: true)

 }

否则你可以试试这个方法:

if var topVC = getTopViewController() {

  self.window?.rootViewController = topVC
  topVC.present(scoreBoardController, animated: true)

}

尝试让我知道它是否有效。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多