首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从rootViewController内部实例化rootViewController

无法从rootViewController内部实例化rootViewController
EN

Stack Overflow用户
提问于 2015-09-29 13:31:44
回答 2查看 115关注 0票数 0

我的入职过程(登录/注册)位于UINavigationController链之外,但是当登录经过身份验证时,我在app委托中调用一个方法,该方法应该实例化rootViewController,然后推送到它。我试过两件事:

上面的代码片段在appDelegate中是从响应本地通知时被调用的方法中运行的,但在本例中不起作用。

代码语言:javascript
复制
  self.window?.makeKeyAndVisible()
  self.window?.rootViewController?.navigationController?.popToRootViewControllerAnimated(true)

下面是我尝试过的另一种方法:

代码语言:javascript
复制
 let rootViewController = self.window?.rootViewController

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let dashboard = mainStoryboard.instantiateViewControllerWithIdentifier("DashboardViewController") as! DashboardViewController

   rootViewController!.navigationController?.popToViewController(dashboard, animated: true)

这两样都不管用。我做错了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-29 14:21:58

我的登录视图控制器也在我的选项卡条控制器之外,所以这就是我所做的:

在appDelegate中:

代码语言:javascript
复制
    var window: UIWindow?

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


    let storyboard  = UIStoryboard(name: "Main", bundle: nil)
    let loginVC = storyboard.instantiateViewControllerWithIdentifier("LoginVC") as! LoginViewController
    self.window?.rootViewController = loginVC
    self.window!.makeKeyAndVisible()


    return true
}

然后在我的loginViewContoller.swift中有一个方法:

代码语言:javascript
复制
    @IBAction func loginPushed(sender: AnyObject) {


    //other stuff here 

    let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
    let tabBarController = UITabBarController()
    let first = mainStoryBoard.instantiateViewControllerWithIdentifier("firstNavController") as! UINavigationController
    let second = mainStoryBoard.instantiateViewControllerWithIdentifier("secondNavController") as! UINavigationController
    let third = mainStoryBoard.instantiateViewControllerWithIdentifier("thirdNavController") as! UINavigationController

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let controllers = [first, second, third]

    tabBarController.viewControllers = controllers
    appDelegate.window!.rootViewController = tabBarController
    appDelegate.window!.makeKeyAndVisible()

}
票数 2
EN

Stack Overflow用户

发布于 2015-09-29 13:51:40

我认为您不应该调用“popToRootViewController”,您可以重新分配window.rootViewController~

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32845348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档