声明RootViewController后无法添加新视图

时间:2015-08-05 08:41:39

标签: ios swift uiviewcontroller uinavigationcontroller

我最近开始使用swift开发iOS应用程序,并且正在尝试使用this教程制作iBeacon应用程序。

我想将ViewController嵌入到navigationController中,但我不知道在这行代码中应该更改什么:

let viewController:ViewController = window!.rootViewController as ViewController
viewController.beacons = beacons as CLBeacon[]?
viewController.tableView.reloadData()

由于RootViewController不再是ViewController。

注意:代码在AppDelegate中。

2 个答案:

答案 0 :(得分:0)

试试这个:

let navController:UINavigationController = window!.rootViewController as UINavigationController
let viewController:ViewController = navController.viewControllers[0] as ViewConroller

答案 1 :(得分:0)

尝试类似的

var窗口:UIWindow?     var viewController:ViewController?     var navigationController:UINavigationController?

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

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)




    viewController = ViewController()
    navigationController = UINavigationController (rootViewController: viewController!)
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()
    return true
}