【问题标题】:How to use old method of setting rootViewController in AppDelegate using Swift [duplicate]如何使用 Swift 在 AppDelegate 中设置 rootViewController 的旧方法 [重复]
【发布时间】:2020-05-13 21:15:58
【问题描述】:

我刚刚开始了一个新的 iOS 项目。我使用 xcode 11 和 iOS 13 创建了项目。当我创建项目时,我发现为了设置我们自己的 rootController,我们必须使用 sceneDelegate 而不是 AppDelegate。我想问是否有可能使用在 AppDelegate 中设置 rootControllers 的旧方法而不是使用 sceneDelegate。

 import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {


    var window : UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let loginController: HomeViewController = HomeViewController(nibName: "HomeViewController", bundle: nil)

       //        let navController: UINavigationController = UINavigationController(rootViewController: loginController)
               self.window?.rootViewController = loginController
               self.window?.makeKeyAndVisible()
        // Override point for customization after application launch.
        return true
    }
}

【问题讨论】:

  • 您的HomeViewController Main.Stroyboard中是否有单独的笔尖文件?
  • @KeshuR。它是 homeviewcontroller 的一个单独的 nib。

标签: ios swift uiviewcontroller ios13 xcode11


【解决方案1】:

按照这些步骤使用 AppDelegate 并选择退出 SceneDelegate

  1. 转到 Info.plist 并从 Info.plist 中删除 Application Scene Manifest 条目。
  2. 删除 SceneDelegate.swift
  3. 在您的 AppDelegate.swift 文件中添加 var window: UIWindow?
  4. 从 AppDelegate.swift 中删除 UISceneSession 生命周期代码

    import UIKit
    
    @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.
         let mainStoryBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
         let loginController = mainStoryBoard.instantiateViewController(withIdentifier: "HomeViewController")
         self.window?.rootViewController = loginController
         self.window?.makeKeyAndVisible()
         return true
    }
    }
    

确保将“HomeViewController”storyboardID 提供给您的视图控制器。

这就是您的 AppDelegate.swift 文件现在的样子。你已准备好出发!

【讨论】:

  • 我按照您的步骤操作,但现在显示黑屏。
  • 你设置了根视图控制器吗?
  • 是的,我已经设置了 rootController
  • @hussnainahmad 也从AppDelegate.swift中删除UISceneSession Lifecycle方法
  • 我也删除了,但还是黑屏
【解决方案2】:

如果您不想使用 sceneDelegate,那么您可以删除所有 sceneDelegate 并从 info.plist 中删除“Application Scene Manifest”

设置UIWindow变量

var window: UIWindow?

确保您已从info.plist 中删除Application Scene Manifest,并更改视图控制器的背景颜色。

如果您想从应用程序中删除暗模式,则启用您设备的Dark Mode 将此密钥添加到您的info.plist

User Interface Style = Light

【讨论】:

  • 我这样做了,但它显示了。现在黑屏。
  • 您的设备设置暗模式对吗?删除暗模式,否则更改视图控制器的颜色@hussnainahmad
  • 不,我正在使用模拟器。我认为它不在黑暗模式下
  • 好的,也检查一下应用场景清单。我创建了新项目并按照它工作的所有步骤进行操作
【解决方案3】:

你为什么不使用SceneDelegate

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            guard let windowScene = (scene as? UIWindowScene) else { return }
            self.window = UIWindow(frame: windowScene.coordinateSpace.bounds)
           //Make sure to do this else you won't get 
           //the windowScene object using UIApplication.shared.connectedScenes
            self.window?.windowScene = windowScene 
            let storyBoard: UIStoryboard = UIStoryboard(name: storyBoardName, bundle: nil)
            window?.rootViewController = storyBoard.instantiateInitialViewController()
            window?.makeKeyAndVisible()
        }

【讨论】:

  • 因为我们在我们的应用程序中有一个模式要遵循。这种模式是我们公司强加的
猜你喜欢
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多