【问题标题】:Load a controller embed to a UINavigationController from AppDelegate in Swift在 Swift 中从 AppDelegate 加载嵌入到 UINavigationController 的控制器
【发布时间】:2015-01-27 23:05:36
【问题描述】:

我需要从 AppDelegate 显示嵌入到 UINavigationController 的表格视图。

我通常使用

    let viewController: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("prova") as ViewController
    window?.rootViewController?.presentViewController(viewController, animated: false, completion: nil)

但是它不支持embed controller,这个代码需要怎么适配?

Here is a test project.

【问题讨论】:

    标签: ios uitableview swift uinavigationcontroller appdelegate


    【解决方案1】:

    您需要使用情节提要中的标识符实例化UINavigationController。导航控制器应该连接到它在故事板中的 rootViewController 并会自动显示它。

    let navController: UINavigationController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("provaNavController") as UINavigationController
    window?.rootViewController?.presentViewController(navController, animated: false, completion: nil)
    

    更新

    由于您没有在情节提要中设置初始视图控制器,因此请使用以下代码:

    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    let navController: UINavigationController = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("provaNavController") as UINavigationController
    window?.rootViewController = navController
    window?.makeKeyAndVisible()
    

    您还需要从常规项目设置中清除 Main.storyboard:

    【讨论】:

    • 否定,它不起作用,在我的故事板中没有设置初始控制器。我用项目测试更新了问题。
    • 是啊,我从来没想过要从App Target清除主界面,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 2012-04-29
    • 2015-02-27
    相关资源
    最近更新 更多