【问题标题】:Programmatically change rootViewController of storyBoard以编程方式更改storyBoard的rootViewController
【发布时间】:2014-05-04 10:36:12
【问题描述】:

我使用Storyboards 创建了我的项目。根ViewController 位于Storyboard 中,我没有在appDelegate 中写过任何代码。

现在我想展示我的应用程序的游览,所以我想将根 ViewControllerTab Bar 更改为我的 TourVC 并且当应用程序游览完成后,我想再次切换回我的根 @ 987654327@转Tab Bar

于是我上网查了一下,按照以下几点进行

1) 从 app.plist 文件中删除 Storyboards, 2) 取消选中 Storyboards 中的选项“isInitialViewController”,在 Tab Bar 控制器的情况下进行检查,因为它是根 ViewController, 3) 在 appDelegate.m 文件中添加此代码。

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ProductTourViewController *PT = [[ProductTourViewController alloc] initWithNibName:@"ProductTourViewController" bundle:nil];
self.window.rootViewController = PT;
[self.window makeKeyAndVisible];
return YES;

但是我的应用程序崩溃并显示此错误日志,

[ProductTourViewController selectedViewController]: unrecognized selector sent to instance 0x1766a9e0

我也收到了警告,

Unsupported Configuration: Scene is unreachable due to lack of entry points and does not have an identifier for runtime access via -instantiateViewControllerWithIdentifier:.

【问题讨论】:

    标签: ios uiviewcontroller storyboard


    【解决方案1】:

    目标-C:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UITabBarController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarcontroller"];
    [[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
    

    斯威夫特:

     let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
     let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("tabBarcontroller") as UITabBarController  
       UIApplication.sharedApplication().keyWindow?.rootViewController = viewController;
    

    斯威夫特 3:

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
    UIApplication.shared.keyWindow?.rootViewController = viewController
    

    斯威夫特 5:

    let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
    UIApplication.shared.windows.first?.rootViewController = viewController
    UIApplication.shared.windows.first?.makeKeyAndVisible()
    

    或者就这样:

    let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
    self.view.window?.rootViewController = viewController
    self.view.window?.makeKeyAndVisible()
    

    两者都工作正常!

    【讨论】:

    • 那么如何从 plist 文件中删除 storyBoard 并取消选中 intialViewController。 newRootVC 也不是内幕故事板。
    • 没有情节提要,您的根视图控制器将被更改
    • 你好,现在在应用程序启动时,我将我的productTour VC设置为rootVC,现在当用户退出ProductTour时,我想再次将我的root VC转移到TabBar,怎么做
    • 我已经编辑了代码并提交了编辑。我在我的应用程序中使用它。
    • 只是想注意didFinishLaunchingWithOptions期间应用程序还没有设置keyWindow,所以在这种情况下你应该使用self.window.rootViewController = ...
    【解决方案2】:

    在主故事板中为您的班级设置故事板 ID。

    UIStoryboard *MainStoryboard = [UIStoryboard storyboardWithName:@"Main"
                                                                         bundle: nil];
    
    UINavigationController *controller = (UINavigationController*)[MainStoryboard
                                                                               instantiateViewControllerWithIdentifier: @"RootNavigationController"];
    
    
    LoginViewController *login=[MainStoryboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
                [controller setViewControllers:[NSArray arrayWithObject:login] animated:YES];
    self.window.rootViewController=controller;
    

    【讨论】:

    • 与我的问题相同...请检查
    • 你好@Mayank,你明白了吗。
    • 各位抱歉,ApplicationBecomeActive 中有一些代码导致了这个问题,现在解决了。谢谢大家,你是对的。
    • 你好,现在在应用程序启动时,我将我的productTour VC设置为rootVC,现在当用户退出ProductTour时,我想再次将我的root VC转移到TabBar,怎么做
    【解决方案3】:

    我们可以快速实现如下

    let storyboard = UIStoryboard(name: "StartingPage", bundle: NSBundle.mainBundle())      
    let loginView: SignInVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInVC
    UIApplication.sharedApplication().keyWindow?.rootViewController = loginView
    

    【讨论】:

      【解决方案4】:

      我用这个很简单:

      UIStoryboard *sb = [UIStoryboard storyboardWithName:@"NameOfStoryBoard" bundle:nil];
      UITabBarController *rootViewController = [sb instantiateViewControllerWithIdentifier:@"NameOfTabBarController"];
      [[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
      

      【讨论】:

      • 这段代码 sn-p 与接受的答案有何不同?
      【解决方案5】:

      只是为了补充 Sunny Shah 的答案,这是它的 Swift 3 版本:

              let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
              let viewController: UIViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
              UIApplication.shared.keyWindow?.rootViewController = viewController
      

      【讨论】:

        【解决方案6】:

        Swift 3 代码:

        在 didFinishLaunchingWithOptions Appdelegate 函数中使用下面。 将“HomeViewController”替换为您希望在应用启动时设置为 Root ViewController 的 ViewController。

        self.window = UIWindow(frame: UIScreen.main.bounds)
        
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        
        let initialViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController")
        
        self.window?.rootViewController = initialViewController
        self.window?.makeKeyAndVisible()
        
        return true
        

        【讨论】:

        • 感谢您的想法。这对我有用: func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { if ProcessInfo.processInfo.arguments.contains("XCUITests") { window?.layer.speed = 100.0 // 在模拟器中加速测试大约 3 倍 } return true }
        【解决方案7】:

        Swift 5 + Xcode 11:

        像这样:

        let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
        UIApplication.shared.windows.first?.rootViewController = viewController
        UIApplication.shared.windows.first?.makeKeyAndVisible()
        

        或者像这样:

        let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
        self.view.window?.rootViewController = viewController
        self.view.window?.makeKeyAndVisible()
        

        两者都工作正常!

        【讨论】:

        • 我在我的场景委托 (willConnectTo) 中使用第一个答案,它正在工作,但是当我强行杀死应用程序并再次打开它时,应用程序没有打开。如果在应用程序委托中更改根视图控制器,它就不能正常工作。
        【解决方案8】:

        目标c

        第 1 步:从 info.plist 中删除主故事板

        第 2 步:在界面构建器中将故事板 ID 添加到视图控制器

        第 3 步:将以下代码添加到应用程序委托中的应用程序完成方法

        self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
        
        
        //set main story board
        if( condition){
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardName1" bundle:nil];
            UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
            [[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
            [self window].rootViewController = rootViewController;
            [self.window makeKeyAndVisible];
        }else{
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardName2" bundle:nil];
            UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
            [[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
            [self window].rootViewController = rootViewController;
            [self.window makeKeyAndVisible];
        }
        

        【讨论】:

          【解决方案9】:

          这是一篇旧文章,但我会回复。 我不推荐以下代码。

          let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
          let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
          UIApplication.shared.keyWindow?.rootViewController = viewController
          

          因为您正在创建两个实例。 我建议在适当的 ViewController 中编写以下代码。

          let appDelegate = UIApplication.shared.delegate as! AppDelegate
          appDelegate.window?.rootViewController = self
          

          【讨论】:

            猜你喜欢
            • 2014-01-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-04-25
            • 2018-12-04
            • 1970-01-01
            相关资源
            最近更新 更多