以编程方式更改storyBoard的rootViewController

问题描述 投票:43回答:8

我使用Storyboards创建了我的项目。根ViewController位于Storyboard内,我没有在appDelegate写一个代码。

现在我想展示我的应用程序,所以我想将ViewController的根Tab Bar更改为我的TourVC,当应用程序的浏览完成后,我想再次将我的根ViewController切换回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
8个回答
136
投票

Objective-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

11
投票

在主故事板中为您的班级设置故事板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;

7
投票

在swift中我们可以实现如下

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

6
投票

我用这个简单:

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

3
投票

只是为了添加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

2
投票

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

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

1
投票

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

0
投票

目标c

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

步骤2:在界面构建器中将storyboard id添加到视图控制器

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

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];
}
© www.soinside.com 2019 - 2024. All rights reserved.