gpt4 book ai didi

swift - 第一次启动应用程序 xcode 时出现黑屏

转载 作者:行者123 更新时间:2023-11-30 12:31:56 25 4
gpt4 key购买 nike

我在第一次使用空白屏幕登录应用程序时遇到了一个小问题,并且收到警告“尝试呈现不在窗口层次结构中的 View !”关闭并重新启动后, View 看起来不错。我相信它与 rootViewController 有关,但不确定......提前感谢您的任何帮助或指导!

应用程序委托(delegate)

    @UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var ref:FIRDatabaseReference?
var databaseHandle:FIRDatabaseHandle?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = MainNavigationController()

FIRApp.configure()
ref = FIRDatabase.database().reference()

return true
}

导航 Controller 为 rootViewController

    class MainNavigationController: UINavigationController {

var segmentedController: UISegmentedControl!

override func viewDidLoad() {
super.viewDidLoad()

let vc1 = TravelersFeedVC()
let vc2 = ProfileVC()

if isLoggedIn() {
// assume user is logged in
let homeController = HomeController()
viewControllers = [homeController]
homeController.firstViewController = vc1
homeController.secondViewController = vc2


} else {
perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
}
}

fileprivate func isLoggedIn() -> Bool {
return UserDefaults.standard.isLoggedIn()
}

func showLoginController() {
let loginController = LoginController()
present(loginController, animated: true, completion: {
// perhaps do something here later
})
}
}

//登录函数调用

    func finishLoggingIn() {
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
guard let mainNavigationController = rootViewController as? MainNavigationController else { return }

let vc1 = TravelersFeedVC()
let vc2 = ProfileVC()

if isLoggedIn() {
// assume user is logged in
let homeController = HomeController()
mainNavigationController.viewControllers = [HomeController()]
homeController.firstViewController = vc1
homeController.secondViewController = vc2

} else {
perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
}
UserDefaults.standard.setIsLoggedIn(value: true)
dismiss(animated: true, completion: nil)
}

最佳答案

好吧,所以我的最后一个答案是错误的(我删除了它),问题是应用程序中有一个键窗口,这是你的导航 Controller ,并且你不能在这个窗口上呈现任何内容,直到它加载其 subview (即使它没有任何),这会发生在 viewdidappear 上,因此您应该将 viewdidload 中的代码放在那里。

关于swift - 第一次启动应用程序 xcode 时出现黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43421445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com