Theodoros80
Theodoros80

Reputation: 796

Load view (other from the default) from AppDelegate.m

I have a similar problem as the user "Flocked" (see question here: Load an other View from applicationDidFinishLaunching in the AppDelegate), but after reading his post I didn't manage to work it out, maybe my situation is different from his.

I have inside my didFinishLaunchingWithOptions a routine for checking if the application is run for the first time (also taken from another user):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
    }
    else{
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
    }
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {

        NSLog(@"First run");
    }

    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

I can't manage to load another view, InfoView (a view for setup) if firstLaunch occurs. I have tried:

InfoView *infoView = [[InfoView alloc]init];
[self presentViewController:infoView animated:YES completion:nil];

inside if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])

but no luck. Any ideas or help?

Upvotes: 1

Views: 1653

Answers (2)

rdurand
rdurand

Reputation: 7410

You are trying to call [self presentViewController:infoView animated:YES completion:nil]; on self, which is of class UIResponder because you are in your AppDelegate. You should call it on a UIViewController, probably in your situation the viewController.

EDIT : This is the line to call, after you call [self.window makeKeyAndVisible]; otherwise self.window.rootViewController will be nil.

[self.window.rootViewController presentViewController:infoView animated:YES completion:nil];

Upvotes: 0

Felix
Felix

Reputation: 35394

Try this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    if (![defaults boolForKey:@"everLaunched"]) {
        NSLog(@"First run");
        [defaults setBool:YES forKey:@"everLaunched"];
        InfoView *infoView = [[InfoView alloc]init];
        [self.viewController presentViewController:infoView animated:YES completion:nil];    
    }

    return YES;
}

Upvotes: 2

Related Questions