【问题标题】:Swift Code in App Delegate to Load Storyboard Causes CrashApp Delegate 中用于加载 Storyboard 的 Swift 代码导致崩溃
【发布时间】:2015-07-30 18:45:34
【问题描述】:

我正在编写一个通用应用程序,我最初使用两个故事板文件编写它。我在 App Delegate didFinishLaunchingWithOptions 例程中有代码来决定要加载哪个故事板,然后它会消失。从那以后,我意识到这是一件愚蠢的事情,因为我有重复的代码,所以我删除了一个故事板,并使另一个故事板通用。我已经让 VC 指向正确的课程和所有内容。但是,我的应用程序现在拒绝启动。当我在 sim 中运行它时,它给了我错误

如果要使用主故事板文件,应用委托必须实现 window 属性。

.

这是我在 App Delegate 中的代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    var deviceIdiom = UIDevice.currentDevice().userInterfaceIdiom
    if deviceIdiom == UIUserInterfaceIdiom.Phone {
        strDevice = "iPhone"
    } else if deviceIdiom == UIUserInterfaceIdiom.Pad {
        strDevice = "iPad"
    } else {
        strDevice = "Unknown"
    }

    return true
}

我做错了什么?

(我已经看过但没有帮助的问题): Unique Issue displaying first storyboard scene in Xcode
The app delegate must implement the window property if it wants to use a main storyboard file
Managing two storyboards in App Delegate
How to manually set which storyboard view to show in app delegate
Swift - Load storyboard programatically

【问题讨论】:

    标签: ios swift appdelegate xcode-storyboard


    【解决方案1】:

    您的应用委托需要像这样开始:

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
      var window: UIWindow?
    
      //...
    }
    

    【讨论】:

    • 谢谢这是多么愚蠢的错误。非常感谢@GoZoner
    猜你喜欢
    • 1970-01-01
    • 2016-10-06
    • 2016-03-28
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多