创建一个新的默认启动视图控制器

时间:2014-04-12 17:48:57

标签: ios xib viewcontroller

我在线购买了一个测验应用模板,当应用启动MainViewController时,它上面有问题类别。我想添加一个主菜单但是当我添加一个新的视图控制器(称为MenuViewController.h,.m和xib)时,我无法弄清楚如何将它设置为默认的ViewController。我也没有使用故事板我有每个ViewController的.xib文件。请帮忙。

在AppDelegate.m

中有什么
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.

    DefaultSHKConfigurator *configurator = [[QuizSHKConfigurator alloc] init];
    [SHKConfiguration sharedInstanceWithConfigurator:configurator];

    MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:mainViewController] autorelease];
    [mainViewController release];
    self.window.rootViewController = navigationController;
    self.window.backgroundColor = [UIColor blackColor];
    [self.window makeKeyAndVisible];
    return YES;
}

2 个答案:

答案 0 :(得分:1)

这是您的代码描述的内容:

MainViewController的实例被设置为UINavigationController实例的内容视图控制器。此UINavigationController实例设置为UIWindow的根视图控制器(允许您的应用在屏幕上显示的主要组件)。

从概念上讲,它看起来像这样:
MainViewController - > UINavigationController - > UIWindow - >显示在屏幕上。

如果要更改“默认”内容,则需要将MainViewController替换为类别视图控制器,或绕过UINavigationController并设置{{1}的根视图控制器}到您的类别视图控制器。

基本上是这样的:

UIWindow

或者:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.

    DefaultSHKConfigurator *configurator = [[QuizSHKConfigurator alloc] init];
    [SHKConfiguration sharedInstanceWithConfigurator:configurator];

    MenuViewController *menuViewController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
    UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:menuViewController] autorelease];
    [mainViewController release];
    self.window.rootViewController = navigationController;
    self.window.backgroundColor = [UIColor blackColor];
    [self.window makeKeyAndVisible];
    return YES;
}

答案 1 :(得分:0)

您可以通过转到AppDelegate.m的顶部并添加来显示MenuViewController.h #import "MenuViewController.h"

然后在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

你应该替换 MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];

通过 MenuViewController *mainViewController = [[MenuViewController alloc] initWithNibName:nil bundle:nil];