AppDelegate类没有初始值设定项

时间:2017-03-26 20:31:19

标签: ios swift

我从这里下载了示例代码,用于位置跟踪应用https://www.pubnub.com/blog/2015-05-05-getting-started-ios-location-tracking-and-streaming-w-swift-programming-language/。我正在尝试运行应用程序,但在AppDelegate类中,我收到一条错误,说“类AppDelegate没有初始化程序”。导致此错误的原因是什么?如何解决?

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    // MARK: - Properties
    //var window: UIWindow?
    var window = UIWindow(frame: UIScreen.mainScreen().bounds)

    // MARK: - App Life Cycle

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Adding a Navigation Controller and tool bar
        self.window.rootViewController = UINavigationController(rootViewController: MainViewController(nibName: nil, bundle: nil))

        // Make window visible
        self.window.makeKeyAndVisible()

        return true
    }
}

1 个答案:

答案 0 :(得分:1)

我会将窗口设置为可选值,没有默认值。就像你原来那样注释掉了

var window: UIWindow?

然后给窗口一个值,并在应用启动时以编程方式添加根视图控制器

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let viewController = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
let navigationController = UINavigationController(rootViewController: viewController)

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()

return true

}