故事板:在运行时设置初始视图控制器

时间:2014-12-01 07:48:33

标签: ios uiviewcontroller storyboard nib

我正在开发一个iOS 7+应用程序,并且我被告知要为应用程序设置不同的初始视图,具体取决于我在运行时之前不会知道的参数。其中一个可能的初始UIViewControllerUITabBarViewController,另一个是UINavigationController

是否可以使用storyboard进行管理?或者它是使用分隔的nib文件的唯一方法吗?

由于

3 个答案:

答案 0 :(得分:2)

不需要使用单独的nib文件,我通过遵循AppDelegate中的代码

来做同样的事情
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIStoryboard *storyboard = self.window.rootViewController.storyboard;
    if(condition1) {
        UITabBarController *rootViewController= [storyboard instantiateViewControllerWithIdentifier:@"TabbarController"];
        [self setRootViewController:rootViewController];
    } else if(condition2) {
        UIViewController *rootViewController= [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
        [self setRootViewController:rootViewController];
    } else {
        UIViewController *rootViewController= [storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
        [self setRootViewController:rootViewController];
    }
}

-(void)setRootViewController:(UIViewController *)rootViewController {
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
}

答案 1 :(得分:0)

您可以在storyboard中指定UITabBarViewController和UINavigationController的ID。然后手动设置应用程序的rootViewController,如下面的代码所示。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [board instantiateViewControllerWithIdentifier:@"TabBarController"];
[self.window makeKeyAndVisible];

}

答案 2 :(得分:0)

我不确定我是否理解你,但尝试使用JLRoutes。在一个故事板中将TabBarController设置为初始视图控制器,导航控制器将在其他故事板中初始化。然后使用JLRoutes

在它们之间导航
相关问题