簡體   English   中英

嘗試在登機時運行時應用程序委托出錯

[英]Error in app delegate when trying to run on boarding

我正在嘗試使用紙質入職為我的 iOS 應用程序制作入職屏幕,效果很好,但我在app delegate和 storyboard 中遇到問題。

當我運行我的應用程序時,出現錯誤:

無法為 UIMainStoryboardFile 'Main' 實例化默認視圖 controller - 也許未設置指定的入口點?

模擬器顯示黑屏。 這是否意味着沒有設置根視圖 controller? 在情節提要中,我沒有設置初始視圖 controller,但是當我嘗試將板載屏幕設置為初始視圖 controller 時,該應用程序正常工作,表明入職視圖 Z594C103F2C6E04C3C3D8AB005FAZ1 中沒有錯誤。

這是 AppDeleagte。 另外,我確保 storyboard id 是正確的。

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

  window = UIWindow(frame: UIScreen.main.bounds)

  let sb = UIStoryboard(name: "Main", bundle: nil)

  var initialVC = UIStoryboard(name: "Main", bundle:nil).instantiateViewController(withIdentifier: "Onboarding") as UIViewController

  let userDefaults = UserDefaults.standard

  if userDefaults.bool(forKey: "onboardingComplete") {
    initialVC = UIStoryboard(name: "Main", bundle:nil).instantiateViewController(withIdentifier: "Mainapp") as UIViewController
  }

  self.window!.rootViewController = initialVC
}

這里有兩個問題。

首先,您還沒有將UIWindow設置為鍵和可見。 所以,在你設置你的rootViewController之后添加這行代碼:

window?.makeKeyAndVisible()

此時,您的代碼將可以工作,但您提到的錯誤可能仍然是 output 到控制台,當您運行應用程序時。

創建項目時,Xcode 會對其進行設置,以便自動加載Main storyboard(顯示初始視圖控制器)。

因此,即使您自己以編程方式設置window ,您的應用程序仍在嘗試在主 storyboard 文件中啟動初始視圖 controller,正如項目設置所告訴它的那樣。

要阻止它嘗試這樣做,請打開應用程序目標的“常規”設置,清除“主界面”文本字段的內容,然后按回車鍵保存更改。

這將更新您的項目的info.plist ,刪除Main storyboard file base name鍵/值,這會導致此自動行為在應用啟動時發生。

截圖如下:

在目標的常規設置中更新主界面

這段代碼對我有用:

let onboardingVC = "get your onboarding VC here"

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

我希望這對你有幫助。 如果您遇到任何問題,請發表評論。 很高興能幫助你!

看來您最后錯過了“返回真實”

     var window: UIWindow?

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

          window = UIWindow(frame: UIScreen.main.bounds)
          let sb = UIStoryboard(name: "Main", bundle: nil)
          var initialVC = sb.instantiateViewController(withIdentifier: "Onboarding") as UIViewController
          let userDefaults = UserDefaults.standard
          if userDefaults.bool(forKey: "onboardingComplete") {
            initialVC = sb.instantiateViewController(withIdentifier: "Mainapp") as UIViewController
          }
          self.window?.rootViewController = initialVC
          return true
        }

更新:

我最近創建了一個新項目,似乎遇到了您的問題,無法通過 AppDelegate 以編程方式加載 ViewController。

這對我有用。 自 IOS 13 和 SwiftUI 以來,Apple 引入了一個名為“SceneDelegate.swift”的新文件,用於支持 SwiftUI 功能,

因此,您無需在“AppDelegate.swift”中設置任何內容,而是在“SceneDelegate.swift”中執行此操作

window 已經有一個屬性。

var window:UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
   let yourVCToView = UIStoryboard.storyboardWith....()
   window?.rootViewController = yourVCToView
   guard let _ = (scene as? UIWindowScene) else { return }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM