self.navigationController返回nil(Swift)

时间:2014-12-09 21:25:06

标签: ios swift

以下是我的app delegate中的代码:

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

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

    return true
}

func createInititalViewController() ->  UINavigationController{

    var currentStocksModel = CurrentStocksModel()
    var currentStocksController = CurrentStocksViewController(model: currentStocksModel)

    var navController:UINavigationController = UINavigationController(rootViewController: currentStocksController)

    return navController
}

当我在CurrentStocksViewController中println(self.navigationController)时,它返回nil。我忘记做什么了?

这是我打电话给我的println()

init(model:CurrentStocksModel) {
    super.init()
    self.model = model

    stocksView = CurrentStocksView()
    stocksView.delegate = self
    self.view = stocksView

    println(self.navigationController)
}

1 个答案:

答案 0 :(得分:1)

在您的代码中,您首先通过CurrentStocksViewController创建init,然后使用UINavigationController创建currentStocksViewController

因此,在init的{​​{1}}方法中,没有CurrentStocksViewController。您应该将逻辑移至UINavigationController