以编程方式更改storyBoard的rootViewController

时间:2014-03-26 07:15:34

标签: ios uiviewcontroller storyboard

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

现在我想展示我的应用程序,所以我想将ViewController的根Tab Bar更改为我的TourVC,当应用程序的浏览完成后,我想再次切换将我的根ViewController提交给Tab Bar

所以我在网上查阅并遵循以下几点

1)从app.plist文件中删除Storyboards, 2)取消选中" isInitialViewController"来自StoryboardsTab 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:.

9 个答案:

答案 0 :(得分:134)

目标-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

答案 1 :(得分:11)

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

答案 2 :(得分:7)

在swift中我们可以实现它如下

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

答案 3 :(得分:6)

我用这个简单:

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

答案 4 :(得分: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

答案 5 :(得分: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

答案 6 :(得分:1)

  

Swift 3代码:

在didFinishLaunchingWithOptions Appdelegate函数中使用以下内容。 替换" HomeViewController"使用ViewController,您希望在应用启动时设置为Root ViewController。

<TitledPane>
        <TextArea fx:id="taTop" wrapText="true" editable="false" prefHeight="100"/>
</TitledPane>
<TitledPane>
        <TableView fx:id="tableFrist" minHeight="120" maxHeight="120">
            <columns>
                <TableColumn fx:id="column" prefWidth="200"/>
            </columns>
        </TableView>
</TitledPane>
<ScrollPane AnchorPane.topAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.bottomAnchor="0.0"
        AnchorPane.leftAnchor="0.0">
    <SplitPane orientation="VERTICAL" fx:id="splitPane">
        <TitledPane>
            <TreeTableView fx:id="tableSecond">
                <columns>
                    <TreeTableColumn fx:id="columnTreeS" prefWidth="200"/>
                </columns>
            </TreeTableView>
        </TitledPane>
        <TitledPane>
            <TreeTableView fx:id="tableThird">
                <columns>
                    <TreeTableColumn fx:id="columnTreeT" prefWidth="200"/>
                </columns>
            </TreeTableView>
        </TitledPane>
        <TitledPane>
            <TextArea fx:id="taBot" wrapText="true" editable="false"/>
        </TitledPane>
    </SplitPane>
</ScrollPane>

答案 7 :(得分:1)

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()

两者都很好!

答案 8 :(得分:0)

目标c

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

第2步:在界面构建器

中将故事板ID添加到视图控制器

步骤3:将以下代码添加到app delegate

中的应用程序完成方法
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];
}
相关问题