【问题标题】:Completely black screen on setting initial view controller in AppDelegate在 AppDelegate 中设置初始视图控制器时完全黑屏
【发布时间】:2020-01-19 00:18:48
【问题描述】:

如果 user = nil,我只想进入登录屏幕。否则它会转到应用程序的主/主页部分,该部分由标签栏控制器的第一个视图控制器组成

这是我的应用委托:

import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    if Auth.auth().currentUser == nil && !UserDefaults.standard.bool(forKey: "hasViewedWalkthrough") {

        let initialViewController = storyboard.instantiateViewController(withIdentifier: "WalkthroughViewController") as? WalkthroughViewController
        self.window?.rootViewController = initialViewController


    } else if Auth.auth().currentUser == nil && UserDefaults.standard.bool(forKey: "hasViewedWalkthrough") {

        let initialViewController = storyboard.instantiateViewController(withIdentifier: "loginViewController") as? LoginViewController
        self.window?.rootViewController = initialViewController

    } else {

        let initialViewController = storyboard.instantiateViewController(withIdentifier: "homeTabBarController") as? MainTabBarViewController
        self.window?.rootViewController = initialViewController

    }

    self.window?.makeKeyAndVisible()
    return true
}

我无法进入入职或登录屏幕。我总是被引导到主屏幕。如果我在情节提要中手动将它们设置为初始视图控制器,我知道屏幕呈现良好。

我尝试了许多解决方案,但没有一个有效。

更新

【问题讨论】:

  • 您能否向我们展示您的项目目标页面,在项目层次结构中单击您的项目名称,然后在“目标”下再次单击您的项目名称
  • 它是否在“常规”部分(顶部的水平菜单)下?那里没看到
  • 是的,只需发送该页面的屏幕截图]
  • 已将其添加到主要原始帖子中。
  • 我建议做的是制作一个全新的故事板并使其成为初始视图控制器,在其上添加您的应用程序徽标或其他内容,并使其成为 Splashscreen 之类的东西。将 AppDeleage 文件中的 firebase 逻辑添加到新的视图控制器中,如果登录视图控制器未登录,则对登录视图控制器执行 performseuge(),如果登录,则对应用程序的主页部分执行 performseuge()。这样,您的用户就会对您的应用程序加载有一些视觉反馈。如果您需要任何方面的帮助,我可以为您提供答案以帮助您解决这个问题?

标签: swift firebase firebase-authentication


【解决方案1】:

试试这个:

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController = storyBoard.instantiateViewControllerWithIdentifier("homeTabBarController") as? TabBarViewController // whatever your swift file is called 
        self.window?.rootViewController = storyBoard

为 if 语句的每个语句使用该代码,显然将视图控制器的名称分别更改为登录名称。

【讨论】:

  • 在最后一行中,它应该是 initialViewController 而不是我认为的故事板。我试过这个,我不再黑屏了。但即使处于注销状态,即 Auth = nil,我最终也会进入应用程序的主要部分而不是登录屏幕。
  • 好的,那里的答案解决了您的黑屏问题?再次为我更新您的新代码,更新您的问题...
  • @BVB09 我不知道这是否是问题所在,但我有一种感觉,您已将initalViewController 用于视图控制器的所有三个分配,请尝试更改他们以自己的名义。例如,第一个给let WalkthroughViewController,下一个给let loginViewController,第三个给let homeTabBarViewController,而不是把三个都写成initalViewController
  • 不,这不是问题所在。我重复了最初在它们内部的 if 块之外的代码,结果是一样的。
  • 在顶部有FirebaseApp.configure() 添加print(Auth.auth().currentuser 并告诉我它在说什么
【解决方案2】:

对于将来阅读本文的人,iOS 13 引入了 SceneDelegate,现在所有与 UI 相关的东西都应该从那时开始配置。 我遇到了同样的问题并通过使用此代码解决了它。 现在应该从这里配置自定义窗口。

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let sceneWindow = (scene as? UIWindowScene) else { return }

    window = UIWindow(frame: sceneWindow.coordinateSpace.bounds)
    window?.windowScene = sceneWindow
    window?.makeKeyAndVisible()

    if let navigationController = Storyboard.Main.initialViewController as? UINavigationController,
       let viewController = navigationController.children.first as? CountriesViewController {
        viewController.inject(CountriesViewModel(provider: CountryAPI(),
                                                 informTo: viewController),
                                                 navigator: CountriesNavigator(navigationController: navigationController))

        window?.rootViewController = navigationController
    }
}

【讨论】:

    猜你喜欢
    • 2015-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    相关资源
    最近更新 更多