Xcode以编程方式在AppDelegate.m中加载不同的ViewControllers

时间:2015-03-12 05:35:43

标签: ios objective-c iphone xcode uiviewcontroller

这个问题可能是一个转贴,但我看过类似的问题,看起来都过时了。我是Xcode和iPhone应用程序开发的新手。我有不同的rootViewControllers,我想加载。一个是第一次使用应用程序,然后是常规应用程序。到目前为止,这是我设置的代码:

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
    // app already launched
}
else
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
    [[NSUserDefaults standardUserDefaults] synchronize];


    // This is the first launch ever
}

我不确定如何导航到最初的ViewController,因为每当我这样做时,它都无法工作。应用程序总是在我的其他ViewControllers中的不同代码行中断,这与此无关。虽然,当我再次测试它时,它起作用,因为Bool设置为YES。

基本上,我怎么能以编程方式设置rootViewController,因为在故事板中,我已经将它设置为我想要每次加载的主ViewController,除了第一次打开应用程序。

请提供示例代码,因为我不是编码员的好处(我是新手)。我试图加载的第一个视图控制器" else" section被称为ViewController,我想加载的普通部分称为Tab Bar Controller(标签中的2个视图)。

请不要将此问题标记为重复,因为虽然我已经看到了一些答案,但它们似乎都不适用于iOS 8上的Xcode 6。

5 个答案:

答案 0 :(得分:0)

像这样使用

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
    // app already launched
    self.window.rootViewController = //your ViewController 
    [self.window makeKeyAndVisible];
}
else
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
    [[NSUserDefaults standardUserDefaults] synchronize];


    // This is the first launch ever

    self.window.rootViewController = //your ViewController 
    [self.window makeKeyAndVisible];

}

它会帮助你。

答案 1 :(得分:0)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];

    UIViewController *rootVC = nil;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) {
        // app already launched
        rootVC = [YourFirstVC alloc] init];
    }
    else {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        // This is the first launch ever
        rootVC = [YourSecondVC alloc] init];
    }
    self.window.rootViewController =  rootVC;
    [self.window makeKeyAndVisible];
    return YES;
}

还有一个很好的做法是将密钥存储在静态字符串中,如:

static NSString *const kHasLaunchedOnce = @"HasLaunchedOnce";

当您在键名中输入拼写错误时,它可以避免出现问题。

答案 2 :(得分:0)

试试这个

{

 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.window.backgroundColor = [UIColor blackColor];

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])

{

    // app already launched

}
      else

{


 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];

    [[NSUserDefaults standardUserDefaults] synchronize];


 UIViewController *yourVc = [[UIViewController alloc]init];

     UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:yourVc];
    self.window.rootViewController=navController;
    [self.window makeKeyAndVisible];


    // This is the first launch ever
}

}

答案 3 :(得分:0)

首先从Main.storyboard中删除Storyboard入口点。如图所示

enter image description here

然后,您需要在App委托didFinishLaunching函数中实例化适当的视图控制器。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

UIViewController *rootViewController = nil;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
    // app already launched
    rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"RegularViewController"];
}
else
{
    // This is the first launch ever
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"VirginViewController"];
}

self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];

return YES;

}

请记住。您需要更改视图控制器标识符。 (RegularViewController,VirginViewController)

答案 4 :(得分:-1)

将此行更改为您在应用中所需的颜色。

self.window.backgroundColor = [UIColor blackColor];

对于白色,

self.window.backgroundColor = [UIColor whiteColor];